我已经这样声明了operator=
:
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;
但是编译器不喜欢定义的签名。错误消息是:
HashTable: suer of class template requires template argument list
HashTable<HashElement>::operation=': unable to match function definition or an existing declaration
在网上看到的东西应该是我写的方式。怎么了?
最佳答案
您必须将模板参数列表添加到返回类型,如错误消息所示:
HashTable<HashElement>& HashTable<HashElement>::operator=(const HashTable& aTable)
该参数不需要模板参数列表,因为编译器已经知道您正在定义
HashTable<HashElement>::operator=
,您可以在其中使用injected-class-name。在类模板定义中声明
operator=
的情况相同。您可以在此处省略返回类型模板参数,但在类外部进行定义时则不能。