AC130
AC130

Reputation: 27

Overloading operator >>

I have a template and I can't overload operator >> from cout.

template <class C> class Fract {
/*some fields*/
friend std::ostream& std::operator<<(std::ostream& out, Fract f);
};

When I write it, gcc (CodeBlocks) write me, that this function isn't template (non-template), but how I know, template scope is spread from { to }. How is it? I try other code:

template <class C> class Fract {
/*some fields*/
std::ostream& std::operator<<(Fract f);
};

And it isn't work. I think I must use something like that:

std::ostream<C>& std::operator<<(Fract f);

But I can't write it right. Can you help me?

UPD: Thank you, in real programm this mistakes aren't present!

Upvotes: 0

Views: 101

Answers (1)

P0W
P0W

Reputation: 47794

You should define it like following outside of class Fract

template <class C>
std::ostream& operator <<(std::ostream& out, const Fract<C>& f)
{
    // ....
    return out ;
}

SEE HERE

Upvotes: 4

Related Questions