xcode 没有模板参数列表就不能引用类模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12993915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot refer to class template without a template argument list
提问by selina
I'm new to C++. This is for my homework and below is the code that was given to us by the professor to help us work on this assignment but it doesn't compile... I've marked the line where error is generated and the error message is
"Cannot refer to template 'hash' without a template argument list".
I'm not sure how to fix it. Can someone point me to the right direction, please?
(I've removed the lines that, I assume, is irrelevant to the error message.)
我是 C++ 的新手。这是我的作业,下面是教授给我们的代码,用于帮助我们完成这项作业,但它无法编译……我已经标记了生成错误的行,错误消息是“不能在没有模板参数列表的情况下引用模板“哈希””。
我不知道如何解决它。有人能指出我正确的方向吗?
(我已经删除了我认为与错误消息无关的行。)
The class is defined as:
该类定义为:
template <typename HashedObj>
class HashTable
{
public:
//....
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry( const HashedObj & e = HashedObj( ), EntryType i = EMPTY )
: element( e ), info( i ) { }
};
vector<HashEntry> array;
int currentSize;
//... some private member functions....
int myhash( const HashedObj & x ) const
{
int hashVal = hash( x ); <<--- line with error
hashVal %= array.size( );
if( hashVal < 0 )
hashVal += array.size( );
return hashVal;
}
};
int hash( const HashedObj & key );
int hash( int key );
--- and int hash() function in cpp file ----
--- 和 cpp 文件中的 int hash() 函数 ----
int hash( const string & key )
{
int hashVal = 0;
for( int i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key[ i ];
return hashVal;
}
int hash( int key )
{
return key;
}
回答by Seth Carnegie
I suspect you are using namespace std;
(I can tell because you are using vector
without std::
) and there exists a name hash
in the std
namespace, so the names conflict.
我怀疑你是using namespace std;
(我可以告诉,因为你用vector
不用std::
),并存在一个名字hash
在std
命名空间,所以名称冲突。
You should use std::
instead of bringing in the entire std
namespace, especiallyin an header file where an innocent user can #include
it and pollute their program with std
too.
您应该使用std::
而不是引入整个std
命名空间,尤其是在无辜用户可以使用#include
并污染他们的程序的头文件中std
。