重载运算符>>

时间:2014-07-05 09:40:34

标签: c++

我有一个模板,我不能重载运算符>>来自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:谢谢你,在真正的程序中,这个错误不存在!

1 个答案:

答案 0 :(得分:4)

您应该像在课程Fract

之外定义它一样
template <class C>
std::ostream& operator <<(std::ostream& out, const Fract<C>& f)
{
    // ....
    return out ;
}

查看HERE

相关问题