使用类模板需要数组的模板参数列表

时间:2015-01-24 06:07:44

标签: c++ templates

我现在已经这两天了......

我是c ++的初学者,遇到了麻烦。

我只会提供必要的最少代码。

template <typename Type>
class Array {
        public:

        *//stuff*

        Array operator= (Array const &);
};

template <typename Type>
Array& Array<Type>::operator=(Array const &rhs) {       //ERROR #1 here

*//stuff*

}                                                       //ERROR #2 here

我收到以下2个错误

&#39;阵列&#39; :使用类模板需要模板参数列表
&#39; Array :: operator =&#39; :无法将函数定义与现有声明匹配

请帮忙。

提前谢谢

2 个答案:

答案 0 :(得分:4)

定义的返回类型需要明确地拼写为Array<Type> &。这是因为编译器在看到Array<Type>之前不知道您在Array<Type>::成员定义的上下文中,因此在没有模板参数的情况下您不能使用Array

template <typename Type>
Array<Type>& Array<Type>::operator=(Array const &rhs) {
//   ^^^^^^

或者,您可以使用C ++ 11的auto语法来允许使用不带模板参数的Array名称,因为此语法指定编译器后的返回类型知道定义的背景。

template <typename Type>
auto Array<Type>::operator=(Array const &rhs) -> Array& {
//                                               ^^^^^^
// This works because the compiler has already encountered "Array<Type>::"

答案 1 :(得分:0)

首次错误检查

Array<Type>& Array<Type>::operator==(Array<Type> const &rhs)

你不能只使用数组阵列必须有argmunets&lt;&gt;

在班级

Array<Type> operator=(Array <Type> const &){}