2013-07-06 47 views
1

我想第一次实现链式哈希表。我想创建一个列表向量。所以我宣布了一个列表的向量为私人。列表向量

class HashTable{ 
public: 
    HashTable(int) ; 
    void add(int k) ; 
    int remove(int k) ; 
    int find(int k) ; 
private: 
    vector<list> t ; 
    int n ; 
    int hash(int) ; 
}; 

它表示以下错误:

\ HashTable.cpp [错误]键入模板参数列表在参数1 /值不匹配 '的模板类的std ::矢量'

基本上我的问题是List类型,所以如果我们可以声明int的向量,那么为什么不能声明列表的向量?

+0

您应该更改问题的标题以反映实际问题。标题提到deques,但实际上你正在使用列表。 – Borgleader

回答

3

您忘记了列表的模板参数。它不能仅仅是list它必须的东西名单像std::list<int>(其中int可以是任何其他类型,但因为所有其他成员函数使用int,我在我的例子中使用INT)

下面一段代码说明问题:

#include <vector> 
#include <list> 

int main(int argc, char* argv[]) 
{ 
    std::vector< std::list<int> > v; // this compiles 
    std::vector<std::list> v2; // and this doesn't 

    return 0; 
} 

正如您在下面看到的,所有编译错误都来自第7行(列表中缺少模板参数的错误)。 This was compiled on ideone

prog.cpp: In function ‘int main(int, char**)’: 
prog.cpp:7:26: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’ 
prog.cpp:7:26: error: expected a type, got ‘list’ 
prog.cpp:7:26: error: template argument 2 is invalid 
prog.cpp:7:30: error: invalid type in declaration before ‘;’ token 
prog.cpp:7:28: warning: unused variable ‘v2’ [-Wunused-variable] 
1

您必须指定列表应包含哪些类型作为元素。你不能只说“一个清单”,你必须说“一些T型清单”。列表模板需要一个参数,就像向量一样,所以你必须说一些类似于std::vector<std::list<char*> >的东西。如果不使用C++ 11,请小心在第一个>之后留出空格,否则它将不起作用,因为它被解析为>>运算符。