将节点作为输出运算符传递
这会打印一条有关限定符的错误消息,但并不真正理解这意味着什么以及如何调整代码以使其工作?无论如何,非常感谢您查看代码。
注意:ostream 运算符在 Node 类中是友元。
using namespace std;
ostream& operator(ostream& output, const Node* currentNode)
{
return output;
}
void Node::nodeFunction()
{
//This node has items attached to the 'this' statement. After
//the necessary functions, this is called to output the items.
cout << this;
}
This prints an error message about qualifiers but don't really understand what that means and how to adjust the code for it to work? Anyways, thanks a lot for looking at the code.
Note: The ostream operator is friended in the Node class.
using namespace std;
ostream& operator(ostream& output, const Node* currentNode)
{
return output;
}
void Node::nodeFunction()
{
//This node has items attached to the 'this' statement. After
//the necessary functions, this is called to output the items.
cout << this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的重载流运算符声明应如下所示:
您应该返回对
std::ostream
对象的引用,&
错误地放置在您的重载函数原型中。请查看此处的工作示例。
为了完整起见,在此处添加源代码。
注意:为了便于演示,我已将类
Node
成员设为 public。Your overloaded stream operator declaration should be like this:
You should be returning a reference to object of
std::ostream
, the&
is wrongly placed in your overloaded function prototype.Have a look at the working sample here.
Adding the source code here, for completeness.
Note: I have taken class
Node
members as public for ease of demonstration.运算符返回值上的
&
位置错误,对于 ostream 运算符,通常使用引用而不是指针更好:The
&
on the return value of the operator is in the wrong place, and it's generally better to use references rather than pointers for ostream operators: