类模板,没有构造函数可以获取源(字符串)
Class template, no constructor could take the source(string)
尝试编译代码会给出几个错误(底部的错误代码)
//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\documentsvisual studio 2013projectspa 3 templatespa 3 templatesheap.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\documentsvisual studio 2013projectspa 3 templatespa 3 templatesheap.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\documentsvisual studio 2013projectspa 3 templatespa 3 templatesdriver.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
(尽管您添加的所有值都是数字,但让我们跳过这一刻)。
因此,bubble_up
的参数也将是一个字符串:
TYPE Heap<TYPE>::bubble_up(TYPE pos){
问题像这样来:
if (pos == 1)// root of tree
而这个:
heap[pos / 2] = heap[pos];
在第一种情况下,您正在尝试将字符串与数字进行比较,并在第二种情况下尝试对字符串执行除法!这是行不通的。如果您尝试传入结构和类以及其他任何内容,问题只会变得更糟。
您要么需要强制 TYPE 是可以对其执行这些操作的整数类型,要么需要认真重写堆的工作方式。
要允许泛型堆在提供非整数TYPE
时失败并出现明显错误,您可以这样做:
template<class TYPE>
class Heap{
private:
static_assert(std::is_integral<TYPE>::value, "Must be an integral type");
如果你真的希望你的堆与字符串一起工作......井。这完全是另一个问题。您可以尝试存储所有值的哈希值:
#include <functional>
// ...
std::hash<TYPE> hashfunc;
size_t hashval = hashfunc(pos);
哈希函数是为所有标准库类型定义的,并返回一个很好的整数size_t
值,我认为该值可以提供给堆算法的其余部分,而无需进行任何实质性更改。如果您想从 bubble_up
等返回有用的值,您仍然需要保留从哈希值到原始数据的映射,但我会将其作为练习留给读者。
相关文章:
- 获取字符串的长度并将其分配给数组
- 调用CreateProcess()并获取字符串的返回值
- 你如何从 Boost Spirit X3 词法解析器中获取字符串?
- 使用 ReadProcessMemory 获取字符串值的访问冲突
- 如何从输入中获取字符串向量?
- 如何获取字符串输入n次?
- property_tree获取字符串形式的数组值
- 使用 ctypes 和 wchar_t 时如何从 C++ 获取字符串到 python?
- 获取字符串大小的各种方法之间的比较
- 方法获取字符串
- 在getline()之后获取字符串
- 为什么我的拆卸C 代码使用指令指针和偏移来获取字符串文字
- 无法使用 Poco 获取字符串的路径
- 如何在qtablewidget中获取字符串的行号
- 如何获取字符串和字节进行比较
- 恒定字符阵列的获取字符串长度函数(strlen)不是恒定表达式
- 我需要创建一个函数来获取字符串中的这些数字
- 如何从地址获取字符串?
- 为什么即使尚未初始化,获取字符串[0]也是有效的?
- 获取C++字符串的位