C++类模板复制构造器返回类型
C++ class template copy contructor return type
我这样声明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=
,您可以在其中使用注入的类名。
同样适用于在类模板定义中声明operator=
。您可以在此处省略返回类型模板参数,但在类外部定义时则不能。
相关文章:
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 为什么协程的返回类型必须是可移动构造的?
- unique_ptr具有右值删除器的构造函数返回 null
- 任何返回类型的可变参数函数包装器
- 推导成员函数包装器的返回类型时出错
- 使用 std::function 时选择自动返回类型而不是构造函数的调用运算符
- C 中构造函数的返回类型是什么
- C++ 中构造函数的返回类型
- C++错误 C2533,ctor :构造函数不允许返回类型
- 当我定义一个具有类(非引用)返回类型的函数时,调用类的默认构造函数或复制构造函数
- 构造函数没有返回类型,但为什么此部分可以正常编译
- 制作解析器以提取函数名称、参数、返回类型
- 函数返回类型名列表:<T>:迭代器有什么问题?
- 请求返回类型的构造函数
- C++类模板复制构造器返回类型
- 返回类型 std::bind 隐式转换为两个不同的显式构造函数
- 是的,构造函数返回是当前实例,但我们不能使用返回类型.是真的吗
- 双取消引用迭代器的返回类型
- 众所周知,构造函数没有返回类型,但在下面的代码中,构造函数正在返回..任何人都能解释吗
- c++迭代器返回类型