ostream 运算符 <<不执行
所以我自己编写了这段代码,但取自其他示例代码...
class A{
friend std::ostream& operator<< (std::ostream& out, A& a);
// Constructors, destructor, and variables have been declared
// and initialized and all good.
}
std::ostream& operator<< (std::ostream& out, A& a){
out << " this gets written " << endl; // it doesn't get executed
return out;
}
int main(){
A *_a = new A();
return 0;
}
好吧,这只是不在控制台中打印 " this gets write "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试通过 std::cout << 使用运算符a 或类似的东西,问题是您将指针传递给对象,而
<<
运算符被定义为采用 >对对象的引用。您需要将a
声明为常规(非指针)A
,或者使用std::cout << *a
。If you're attempting to use the operator via
std::cout << a
or something similar, the problem is you're passing a pointer to an object, while the<<
operator is defined as taking a reference to an object. You either need to declarea
as regular (non-pointer)A
, or usestd::cout << *a
.