I got 2 classes that are using templates with 2 arguments, MinHeap and HeapNode. I create a MinHeap object in the main that creates a vector inside the class and after that I call insert function that inserts a HeapNode in the MinHeap vector.
The problem appears when in insert method I can't create a HeapNode because the variable types are not caught by the compiler giving me that error:
type/value mismatch at argument 1 in template parameter list for 'template class HeapNode' Heap.push_back(HeapNode(key,value));
Main code:
MinHeap<int,string> vector();
vector.insert(2,"Hola");
Insert function code:
void MinHeap<T,V>::insert(T key, V value){
Heap.push_back(HeapNode<typeid(key).name(),typeid(value).name()>
(key,value));
}
HeapNode class code:
#ifndef HEAPNODE_H
#define HEAPNODE_H
template <class T, class V>
class HeapNode {
public:
HeapNode(T newKey, V newValue);
HeapNode(const HeapNode& orig);
virtual ~HeapNode();
T getKey();
void setKey(T newKey);
V getValue();
void setValue(V newValue);
private:
T key;
V value;
};
template <class T, class V>
HeapNode<T,V>::HeapNode(T newKey, V newValue){
this->key = newKey;
this->value = newValue;
}
template <class T, class V>
T HeapNode<T,V>::getKey(){
return key;
}
template <class T, class V>
void HeapNode<T,V>::setKey(T newKey){
this->key = newKey;
}
template <class T, class V>
V HeapNode<T,V>::getValue(){
return value;
}
template <class T, class V>
void HeapNode<T,V>::setValue(V newValue){
this->value = newValue;
}
#endif /* HEAPNODE_H */
MinHeap class code:
#ifndef MINHEAP_H
#define MINHEAP_H
#include "HeapNode.h"
#include <vector>
#include <iterator>
#include <typeinfo>
#include <iostream>
using namespace std;
template <class T, class V>
class MinHeap {
public:
MinHeap();
MinHeap(const MinHeap& orig);
virtual ~MinHeap();
T size();
T empty();
void insert(T key,V value);
T min();
T minValues();
void removeMin();
void printHeap();
void removeMinAux(T i);
private:
std::vector<T,V> Heap;
void swap(HeapNode<T,V>* parent, HeapNode<T,V>* child);
};
/*
template <class T, class V>
MinHeap<T,V>::~MinHeap(){
delete Heap;
}*/
template <class T, class V>
void MinHeap<T,V>::insert(T key, V value){
Heap.push_back(HeapNode<typeid(key).name(),typeid(value).name()>(key,value));
}
template <class T, class V>
T MinHeap<T,V>::empty(){
return Heap.empty();
}
template <class T, class V>
T MinHeap<T,V>::size(){
return Heap.size();
}
template <class T, class V>
T MinHeap<T,V>::min(){
return Heap.front().getKey();
}
template <class T, class V>
T MinHeap<T,V>::minValues(){
return Heap.front().getValue();
}
template <class T, class V>
void MinHeap<T,V>::removeMin(){
Heap.front() = Heap.back();
Heap.pop_back();
removeMinAux(0);
}
template <class T, class V>
void MinHeap<T,V>::removeMinAux(T i){
if(Heap.at(i*2+1)== 0){
cout<< "Heap has been reordenated"<<endl;
}else{
if(Heap.at(i*2+1).getKey()<Heap.at(i*2+2).getKey()){
swap(Heap.at(i*2+1),Heap.at(i));
removeMinAux(i*2+1);
}else{
swap(Heap.at(i*2+2),Heap.at(i));
removeMinAux(i*2+2);
}
}
}
template <class T, class V>
void MinHeap<T,V>::swap(HeapNode<T,V>* parent, HeapNode<T,V>* child){
T tmp;
tmp = *child;
*child = *parent;
*parent = tmp;
delete tmp;
}
#endif /* MINHEAP_H */
^
The problems with your code run a bit deep, unfortunately.
Your main code creates a min heap instance like this:
MinHeap<int,string> vector();
This wouldn't build for me; a new version of visual studio and an old version of GCC both rejected it with various complaints (are you aware of the most vexing parse , as an example of how this sort of thing can trip you up?) I changed it to
MinHeap<int, string> v;
This constructs a MinHeap
instance with a private member Heap
that effectively looks like this:
std::vector<int,string> Heap;
Now, if you look at the docs for std::vector you'll see that what you're doing here is creating a vector of int
with an allocator of string
. This isn't ever going to work, and you're certainly not going to be able to push HeapNode
instances onto it!
What you should have done is this:
std::vector<HeapNode<T, V>> Heap;
Now, in your insert function, you do this:
Heap.push_back(HeapNode<typeid(key).name(),typeid(value).name()> (key,value));
Which is, as IInspectable says above, a fundamental misunderstanding of how the typeid operator works, and also a misunderstanding of how template types work. typeid(foo).name()
is the name of a type, not the actual type itself. That's why your error message says "type/value mismatch".
You already know the type of key
and the type of value
... they're right there in the function prototype! T
and V
respectively!
If you do this:
Heap.push_back(HeapNode<T,V>(key,value));
The problem goes away. There may be more problems, but as you haven't defined all the functionality of HeapNode
and MinHeap
in your supplied code, there's not much I can do about those.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.