2016-11-23 9 views
0

テンプレートの使用方法と演算子のオーバーロード方法を学習しています。私はoperator[]をオーバーロードすることに成功しましたが、operator+operator=のオーバーロードに関する問題が発生しました。ここに私のコードは次のとおりです。私がコンパイルしようWhenver演算子の '='と '+'のオーバーロード

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; 
} 

、私はそれらのエラーを取得:

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

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

'operator =' must be a non-static member

は私が間違って何をしているのですか?


EDIT:

私は、コードを更新するために管理してきました:

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

ああ、申し訳ありません、私は忘れました。テンプレートパラメータは、_ "friend"宣言に継承されません。テンプレート friend 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 ::'を削除しましたが、私はまだ最後のエラーを取得している、その演算子は非静的なメンバーでなければなりません。私はそれを非静的にすることができますか?実際には、私は私の演算子[] definitonを見てきましたが、同じミスもありましたが、何らかの理由でコンパイラは私にこれをさせました:Pこれはまた、 '非静的メンバー'エラーを生成します:C – Executor1909

+0

@ Executor1909もっと読むスタンダードからの引用。オペレータは1つのパラメータしか持たない。 –

+0

はい、私は知っていますが、以前のエラーが消えるかどうかを確認するためだけに変更しました。 「非静的」エラーがまだ発生します – Executor1909

0

非スタティックメンバーとして実装されている演算子は、右側のオペランドの入力パラメータを1つだけ受け入れる必要があります。左側のオペランドは、オペレータが呼び出されているオブジェクトです。

スタティックメンバーまたは非メンバーとして実装されている演算子は、2つの入力パラメーター、左側および右側のオペランドを受け入れる必要があります。

operator=は2つの入力パラメータを持つ非スタティックメンバーとして宣言されていますが間違っています。

また、operator+は、2つの入力オブジェクトのコピーである新しいオブジェクトを返すためのものです。オペレータが呼び出されているオブジェクトへの参照を返さないでください。 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; 
} 
関連する問題