2016-11-23 138 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; 
}