2016-11-23 137 views
0

我正在学习如何使用模板以及如何重载运算符。我设法超载operator[],但我遇到了超载operator+operator=的问题。这里是我的代码:重载运算符'='和'+'

template <class T> 
class A 
{ 
public: 
    //... 
    friend A<T>& A<T>::operator+ (A<T>&, const A<T>&); 
    friend A<T>& A<T>::operator= (A<T>&, const A<T>&); 
}; 

template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right) 
{ 
    //some functions 
return left; 
} 

template<class T> A<T>& A<T>::operator= (A<T>& left, const A<T>& right) 
{ 
    //some functions 
    return left; 
} 

Whenver我尝试编译,我得到这些错误:

'+': is not a member of 'A<T>'

'=': is not a member of 'A<T>'

'operator =' must be a non-static member

我在做什么错?


编辑:

我已经成功地更新代码:

template <class T> 
class A 
{ 
public: 
    //... 
    A<T> operator+ (A<T>); 
    A<T> operator= (A<T>, const A<T>); 
}; 

template<class T> A<T> A<T>::operator+ (A<T> right) 
{ 
    //some functions 
    return *this; 
} 

template<class T> A<T> operator= (A<T> right) 
{ 
    //some functions 
    return *this; 
} 

貌似operator+作品现在很好,但是编译器会发出此错误:

'operator=' must be a non static member

为什么它是一个静态成员,我该如何解决它?

+0

在函数定义中删除'一个 ::'范围。 –

+0

对不起,我忘了。模板参数不会被“继承”到“朋友”声明中。您必须将它们声明为模板朋友A &operator +(A &,const A &);' –

+0

您确定吗?我得到了现在编译器的内部错误:P 没关系,它现在仍然产生'不成员'的错误:/ – Executor1909

回答

1

对于初学者赋值运算符必须为非静态成员函数

从C++标准(13.5.3分配)

1 An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8), a base class assignment operator is always hidden by the copy assignment operator of the derived class.

其次(11.3好友)

1 A friend of a class is a function or class that is given permission to use the private and protected member names from the class. A class specifies its friends, if any, by way of friend declarations. Such declarations give special access rights to the friends, but they do not make the nominated friends members of the befriending class.

因此例如这个定义

template<class T> A<T>& A<T>::operator+ (A<T>& left, const A<T>& right) 
         ^^^^^ 
{ 
//some functions 
return left; 
} 

不正确。至少您应该删除A<T>::,因为操作员不是班级的成员。

+0

是的,我完全忘了它。所以现在我删除了朋友和'A ::',但我仍然得到最后一个错误,那operator =必须是非静态成员。我怎样才能使它不是静态的?其实,我已经看过我的operator [] definiton,但也有同样的错误,但由于某种原因编译器让我这样做:P现在它也产生这个'非静态成员'错误:C – Executor1909

+0

@ Executor1909 Read more more标准报价。操作员只能有一个参数。 –

+0

是的,我知道,但我改变了,只是为了检查那些以前的错误是否会消失,现在他们只有一个参数。 '非静态'错误仍然发生 – Executor1909

0

作为非静态成员实现的运算符只能接受1个输入参数 - 右侧操作数。左边的操作数是运算符被调用的对象this

作为静态成员或非成员实现的运算符必须接受2个输入参数(左侧和右侧操作数)。

您的operator=被声明为具有2个输入参数的非静态成员,这是错误的。

此外,operator+是为了返回一个新的对象,它是两个输入对象的副本添加在一起。不要返回对调用操作符的对象的引用。而operator=是为了返回对被分配的对象的引用。

试试这个:

template <class T> 
class A 
{ 
public: 
    //... 
    A<T> operator+(const A<T>&) const; 
    A<T>& operator=(const A<T>&); 
}; 

template<class T> A<T> A<T>::operator+(const A<T>& right) const 
{ 
    A<T> result(*this); 
    //some functions to add right to result as needed... 
    return result; 
} 

template<class T> A<T>& A<T>::operator=(const A<T>& right) 
{ 
    // some functions to copy right into this... 
    return *this; 
}