类模板,没有构造函数可以获取源(字符串)

Class template, no constructor could take the source(string)

本文关键字:获取 字符串 构造函数      更新时间:2023-10-16

尝试编译代码会给出几个错误(底部的错误代码)

//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 等返回有用的值,您仍然需要保留从哈希值到原始数据的映射,但我会将其作为练习留给读者。