不能在没有模板参数列表的情况下引用类模板
Cannot refer to class template without a template argument list
我是C++新手。这是我的家庭作业,下面是教授给我们的代码,以帮助我们完成这项作业,但它无法编译...... 我已经标记了生成错误的行,错误消息是 "没有模板参数列表,则无法引用模板'哈希'"。
我不知道如何解决它。 有人可以指出我正确的方向吗?
(我已经删除了我认为与错误消息无关的行。
该类定义为:
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 );
--- 和 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;
}
我怀疑您using namespace std;
(我可以判断,因为您使用的是没有std::
vector
),并且std
命名空间中存在一个名称hash
,因此名称冲突。
您应该使用std::
而不是引入整个std
命名空间,尤其是在头文件中,无辜用户可以#include
它并用std
污染他们的程序。
相关文章:
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 为什么或在什么情况下,你会将参数作为C++中的引用(或指针)传递给函数?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何在开关的情况下使用右值引用
- 为什么 lambda nullptr 取消引用在这种情况下有效?
- 在不使用模板的情况下获取 std::array 引用
- 有没有办法在不使用命名空间 std 或前缀 std:: 的情况下引用 cout?
- 在没有引用传递资源的情况下,如何在java中简化这些代码
- Python在不引用类名的情况下调用类函数
- 为什么C++默认情况下不移动构造右值引用?
- 寻找有关为什么此C++代码在没有引用的情况下不起作用的解释
- 在这种情况下,如何返回对数组中元素的引用?
- 具有仅在某些情况下引用外部变量的属性的类
- 不能在没有模板参数列表的情况下引用类模板
- 如何在不知道特定类型的情况下引用变量
- 如何在不使用 variable_name.at() 的情况下引用字符串的最后一个字符
- 为什么在某些情况下不能在没有强制转换的情况下引用公共静态常量变量?
- 如何在不创建新对象的情况下引用 substr
- 在c++中是否有一个很好的理由在不改变属性的情况下引用它?