将节点作为输出运算符传递

发布于 2024-11-24 04:57:17 字数 444 浏览 5 评论 0原文

这会打印一条有关限定符的错误消息,但并不真正理解这意味着什么以及如何调整代码以使其工作?无论如何,非常感谢您查看代码。

注意: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-12-01 04:57:18

您的重载流运算符声明应如下所示:

std::ostream& operator<<(std::ostream& os, const T& obj);
^^^^^^^^^^^^^

您应该返回对 std::ostream 对象的引用,& 错误地放置在您的重载函数原型中。

请查看此处的工作示例。

为了完整起见,在此处添加源代码。
注意:为了便于演示,我已将类 Node 成员设为 public。

#include<iostream>

using namespace std;

class Node
{
    public: 
    int i;
    int j;
    void nodeFunction();

    friend ostream& operator <<(ostream& output, const Node* currentNode);     
};

ostream& operator<<(ostream& output, const Node* currentNode)
{
   output<< currentNode->i;
   output<< currentNode->j;

   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;
}

int main()
{
    Node obj;
    obj.i = 10;
    obj.j = 20;

    obj.nodeFunction();

    return 0;
}

Your overloaded stream operator declaration should be like this:

std::ostream& operator<<(std::ostream& os, const T& obj);
^^^^^^^^^^^^^

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.

#include<iostream>

using namespace std;

class Node
{
    public: 
    int i;
    int j;
    void nodeFunction();

    friend ostream& operator <<(ostream& output, const Node* currentNode);     
};

ostream& operator<<(ostream& output, const Node* currentNode)
{
   output<< currentNode->i;
   output<< currentNode->j;

   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;
}

int main()
{
    Node obj;
    obj.i = 10;
    obj.j = 20;

    obj.nodeFunction();

    return 0;
}
夏雨凉 2024-12-01 04:57:18

运算符返回值上的 & 位置错误,对于 ostream 运算符,通常使用引用而不是指针更好:

ostream& operator<<(ostream &output, const Node ¤tNode)
{
    // Output values here.
    return output;
}

void Node::nodeFunction()
{
     cout << *this;
}

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:

ostream& operator<<(ostream &output, const Node ¤tNode)
{
    // Output values here.
    return output;
}

void Node::nodeFunction()
{
     cout << *this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文