pro*_*sor 6 c++ templates operator-overloading visual-studio-2010 friend-function
我有重载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
Run Code Online (Sandbox Code Playgroud)
在主要我只是
#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;
}
Run Code Online (Sandbox Code Playgroud)
哪里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);
Run Code Online (Sandbox Code Playgroud)
但这会产生另一个错误:C2785: 'std::ostream &Polyff::operator <<(std::ostream &,const Polyff::FiniteField<T,n> &)' and '<Unknown>' have different return types
所以我想知道我应该如何更改代码以便编译以及为什么?谢谢!
-------------------------编辑于2012.12.31 -------------------- -------
代码现在用g ++编译.这是github存储库.
我尝试在 Visual C++ 2010 上编译您的代码。我得到了与您相同的错误。
实际上,Visual 不喜欢您的代码中的两件事:
这对我来说是一份工作!
归档时间: |
|
查看次数: |
534 次 |
最近记录: |