重载ostream
我有我的课程,例如 TEST 在 TEST.h 中我有
朋友 ostream&运算符<< (ostream&out, const test&outstr);
在 TEST.cc
ostream&运算符 <<(ostream&out, test&strout) { 出<< “测试”; 返回; }
在主要 测试x; 计数<< x;
我收到错误消息: 错误:未定义引用 `operator<<(std::basic_ostream >&, test const&)
有什么问题?
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;
cout<< x;
I recieve error message:
error: undefined reference to `operator<<(std::basic_ostream >&, test const&)
whats the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在声明中有 const:
friend ostream&运算符<< (ostream&out, const test&outstr);
并且在实现中没有const:
ostream& operator <<(ostream& out, MISSING CONST test& strout)
在实现中添加 const 应该可以解决您的问题。
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.