【问题标题】:Class template, no constructor could take the source(string)类模板,没有构造函数可以获取源(字符串)
【发布时间】: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&lt;std::string&gt; *s_heap = new Heap&lt;std::string&gt;(); 无需动态分配。 C++ 不是 Java Heap&lt;std::string&gt; s_heap; 是你所需要的。
  • bubble_up() 被声明为采用 TYPE 参数,但对它的调用和内部使用清楚地表明它应该采用 int
  • bubble_up() 采用 TYPE 对象(在您的实例中是 std::string),但您提供的是 int

标签: c++ string templates constructor


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多