2016-02-27 83 views
1

我宣布operator=这样的:C++类模板拷贝构造器的返回类型

HashTable& operator=(const HashTable& aTable); 

而且我定义它的类以外的方式如下:

template <typename HashElement> 
HashTable& HashTable<HashElement>::operator=(const HashTable& aTable) 
{ 
    /*Do the copy thing*/ 
    return *this; 
} 

我期望它用下面的代码编译:

HashTable<EngWord> hashTable; 
HashTable<EngWord> hashTableA; 
hashTableA = hashTable; 

但是编译器不喜欢de的签名FINITION。错误信息是:

HashTable: suer of class template requires template argument list 
HashTable<HashElement>::operation=': unable to match function definition or an existing declaration 

我在网上看到的应该是我写的。怎么了?

回答

3

你有一个模板参数列表添加到返回类型,作为错误信息说:是不是需要的参数

HashTable<HashElement>& HashTable<HashElement>::operator=(const HashTable& aTable) 

模板参数列表,因为编译器已经知道你定义HashTable<HashElement>::operator=,其中您可以使用injected-class-name

这同样适用于在类模板定义中声明operator=。您可以在那里省略返回类型模板参数,但不能在类外定义时使用。

+3

另一种选择是使用尾随返回类型 –