【发布时间】:2014-11-17 21:54:49
【问题描述】:
尝试编译代码会出现几个错误(错误代码在底部)
//heap.h
#include <iostream>
#include <vector>
using namespace std;
template<class TYPE>
class Heap{
private:
vector<TYPE> heap;
int size;// number of elements in the heap
bool maxheap = true;
TYPE bubble_up(TYPE item);
TYPE bubble_down(TYPE item);
public:
Heap();
Heap(bool maxheap);
Heap(vector<TYPE>, bool order);
~Heap();
void build_heap();
TYPE Insert(TYPE item);
TYPE Delete(TYPE& item);
const vector<TYPE> sort(bool order);
const vector<TYPE> sort();// defualt sort if no variable given, max sort
TYPE get_size();
void print_heap();
void clear_heap();
};
template<class TYPE>
Heap<TYPE>::Heap(){
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(bool order){
maxheap = order; // true is max, false is min
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
}
template<class TYPE>
Heap<TYPE>::Heap(vector<TYPE> x, bool order){
maxheap = order;// true is max, false is min
TYPE tempSize;
TYPE dummy{};
heap.push_back(dummy);
size = heap.size() - 1;
tempSize = x.size();
for (TYPE y = 0; y < tempSize; y++){
heap.push_back(x[y]);
}
size = heap.size() - 1;
build_heap();
}
template<class TYPE>
TYPE Heap<TYPE>::Insert(TYPE item){
heap.push_back(item);
size = heap.size() - 1;
return bubble_up(size);
}
TYPE Heap<TYPE>::bubble_up(TYPE pos){
TYPE retVal;
if (pos == 1)// root of tree
{
return pos;
}
if (maxheap == true){
if (heap[pos] > heap[pos / 2]){// greater than parent
TYPE temp = heap[pos / 2]; //swap method
heap[pos / 2] = heap[pos];
heap[pos] = temp;
return retVal = bubble_up(pos / 2);
}
else{
return pos;
}
}
if (maxheap == false){//min heap
if (heap[pos] < heap[pos / 2]){// less than parent
TYPE temp = heap[pos / 2]; //swap method
heap[pos / 2] = heap[pos];
heap[pos] = temp;
return retVal = bubble_up(pos / 2);
}
else{
return pos;
}
}
}
这里是当前正在使用的驱动文件。
#include <iostream>
#include <string>
#include "Heap.h"
using std::cout;
using std::endl;
typedef string TYPE;
int main(void) {
Heap<std::string> *s_heap = new Heap<std::string>(); // string heap
std::string s_item = "0";
vector<std::string> s_blah(11, s_item);
cout << "\n*** Test insert elements ***";
cout << endl << s_heap->Insert("15");
cout << endl << s_heap->Insert("1");
cout << endl << s_heap->Insert("3");
cout << endl << s_heap->Insert("4");
cout << endl;
}
完整的错误代码:
c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\heap.h(85): error
C2664: 'std::string Heap<std::string>::bubble_up(TYPE)' : cannot convert argument 1 from 'int' to 'std::string'
1> with
1> [
1> TYPE=std::string
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\heap.h(82) : while compiling class template member function 'std::string Heap<std::string>::Insert(TYPE)'
1> with
1> [
1> TYPE=std::string
1> ]
1> c:\users\\documents\visual studio 2013\projects\pa 3 templates\pa 3 templates\driver.cpp(17) : see reference to function template instantiation 'std::string Heap<std::string>::Insert(TYPE)' being compiled
1> with
1> [
1> TYPE=std::string
1> ]
【问题讨论】:
-
Heap<std::string> *s_heap = new Heap<std::string>();无需动态分配。 C++ 不是 JavaHeap<std::string> s_heap;是你所需要的。 -
bubble_up()被声明为采用TYPE参数,但对它的调用和内部使用清楚地表明它应该采用int。 -
bubble_up()采用TYPE对象(在您的实例中是std::string),但您提供的是int。
标签: c++ string templates constructor