【发布时间】:2012-02-02 20:16:51
【问题描述】:
所以我有这段代码是我自己编写的,但取自其他示例代码......
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 written "
【问题讨论】:
-
你的代码没有意义。您的
<<运算符接受AccountInfo&类型的参数,而不是A*,您甚至没有向我们展示您实际尝试使用该运算符的部分。您省略了太多代码,剩下的代码对调试您的问题毫无用处。 -
使用?我认为这是一种自动的,就像创建对象时一样。另外 AccountInfo 类型是错误的,我现在修复它。
-
“使用”是指“向我们展示您如何尝试将对象写入流”。您不能只声明操作员并期望它做某事,您必须实际调用它。你期望上面的代码做什么?您发布的唯一代码
A *_a = new A();与<<运算符或流无关。它只是创建一个新的A然后退出。 -
嗯,我在类中有
friend std::ostream& operator<< (std::ostream& out, A& a);,我认为每次创建新对象或类似的东西时都会将它写入控制台。 -
我认为是时候使用good book on C++了!
标签: c++ operator-keyword ostream