我有一个模板,我不能重载运算符>>来自cout。
template <class C> class Fract {
/*some fields*/
friend std::ostream& std::operator<<(std::ostream& out, Fract f);
};
当我写它时,gcc(CodeBlocks)写信给我,这个函数不是模板(非模板),但我怎么知道,模板范围从{传播到}。如何? 我尝试其他代码:
template <class C> class Fract {
/*some fields*/
std::ostream& std::operator<<(Fract f);
};
这不行。我想我必须使用类似的东西:
std::ostream<C>& std::operator<<(Fract f);
但我不能写得对。你能救我吗?
UPD:谢谢你,在真正的程序中,这个错误不存在!
答案 0 :(得分:4)
您应该像在课程Fract
template <class C>
std::ostream& operator <<(std::ostream& out, const Fract<C>& f)
{
// ....
return out ;
}
查看HERE