[英]Overload operator<< for template class
我有重载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;
}
其中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存储库。
这似乎按预期工作:
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.n; // I added the field of my Integer class
}
//some other definitions
}
struct Integer{
Integer() : n(0){}
Integer(int nn) : n(nn){}
int n;
};
using std::cout;
using namespace Polyff;
Integer N(5);
int main () {
FiniteField<Integer, &N> f1;
cout<< f1;
}
我刚刚在模板参数中用对象的指针替换了引用,因为指向全局对象(静态或非静态)的指针是在编译时(或至少链接时)已知的信息。 我不知道接受引用的语言。
请注意,在此示例中,将打印0
因为它对应于_val
的默认构造。
我试图在我的Visual C ++ 2010上编译你的代码。我得到了和你一样的错误。
在你的代码中,Visual实际上有两件事情不喜欢:
这使我的工作!
您应该使用普通变量( T n
而不是T& n
)替换引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.