Reputation: 3934
I have a class Color
, that has friend std::ostream& operator<<(std::ostream&, Color&)
. It looks like this:
struct Color
{
Color(CID c, sstr s)
:color(c),
str(s)
{
}
friend sost& operator<<(sost& o, Color& c)
{
o << "\033[1;" << c.color << "m" << c.str << "\033[0m";
return o;
}
CID color;
sstr str;
};
I can call the operator without any issue in all circumstances but in a templated function:
template<typename T>
void print_head(const T& head, sost& o)
{
o << head << "\r";
o.flush();
spaces+=(headSize);
}
I invoke it with print_head<helper::Color>(rsym, o);
with rsym
being a instance of Color
. And I get
error: invalid operands to binary expression ('sost'
(aka 'basic_ostream<char>') and 'const helper::Color')
o << head << "\r";
~ ^ ~~~~
note: in instantiation of function template specialization
'blk::Bouncer::print_head<helper::Color>' requested here
print_head<helper::Color>(rsym, o);
Whats wrong with the template function?
Upvotes: 1
Views: 261
Reputation: 8171
Your operator takes a non const reference but head
is const.
You should change it to
friend sost& operator<<(sost& o, const Color& c)
Upvotes: 2