开发者

Overloading ostream

开发者 https://www.devze.com 2023-02-10 03:59 出处:网络
I have my class for example TEST in TEST.h I have friend ostream& operator<< (ostream& out, const test& outstr);

I have my class for example TEST in TEST.h I have

friend ostream& operator<< (ostream& out, const test& outstr);

in TEST.cc

ostream& operator <<(ostream& out, test& strout) { out<< "TEST"; return out; }

in main test x; cou开发者_运维技巧t<< x;

I recieve error message: error: undefined reference to `operator<<(std::basic_ostream >&, test const&)

whats the problem?


You have const in the declaration:

friend ostream& operator<< (ostream& out, const test& outstr);

and no const in the implementation:

ostream& operator <<(ostream& out, MISSING CONST test& strout)

Adding const to the implementation should solve your issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消