テンプレート クラスの operator<< のオーバーロードに問題があります。私は Visual Studio 2010 を使用しています。これが私のコードです。
#ifndef _FINITEFIELD
#define _FINITEFIELD
#include<iostream>
namespace Polyff{
template <class T, T& n> class FiniteField;
template <class T, T& n> std::ostream& operator<< (std::ostream&, const FiniteField<T,n>&);
template <class T, T& n> class FiniteField {
public:
//some other functions
private:
friend std::ostream& operator<< <T,n>(std::ostream& out, const FiniteField<T,n>& obj);
T _val;
};
template <class T, T& n>
std::ostream& operator<< (std::ostream& out, const FiniteField<T,n>& f) {
return out<<f._val;
}
//some other definitions
}
#endif
主に私はちょうど持っています
#include"FiniteField.h"
#include"Integer.h"
#include<iostream>
using std::cout;
using namespace Polyff;
Integer N(5);
int main () {
FiniteField<Integer, N> f1;
cout<< f1;
}
whereは、必要な特別な機能を備えInteger
た単なるラッパーです。int
ただし、上記のコードをコンパイルすると、エラー C2679 が表示されます。binary '<<' : no operator found which takes a right-hand operand of type 'Polyff::FiniteField<T,n>' (or there is no acceptable conversion)
コードが次のようになるように、フレンド宣言のパラメーターも削除しようとしました。
friend std::ostream& operator<< <> (std::ostream& out, const FiniteField<T,n>& obj);
しかし、これは別のエラーを生成します: C2785:'std::ostream &Polyff::operator <<(std::ostream &,const Polyff::FiniteField<T,n> &)' and '<Unknown>' have different return types
だから、コンパイルできるようにコードをどのように変更すればよいのでしょうか?なぜですか? ありがとう!
------------------------- 2012.12.31 編集 -------------------- -------
コードは g++ でコンパイルされるようになりました。ここに github リポジトリがあります。