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

use of class template requires template argument list for array

这两天我都在...

我是 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 个错误

'Array' : class 模板的使用需要模板参数列表
'Array::operator =' : 无法将函数定义与现有声明相匹配

请帮忙。

提前致谢

第一次错误检查这个

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

不能只使用 Array Array 必须有 argmunets <>

在 class

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

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

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

或者,您可以使用 C++11 的 auto 语法来允许使用没有模板参数的 Array 名称,因为此语法指定 return 类型 after 编译器知道定义所在的上下文。

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