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

2 に答える 2

0

演算子の戻り値の&onが間違った場所にあるため、一般に、ostream演算子のポインターではなく、参照を使用することをお勧めします。

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

void Node::nodeFunction()
{
     cout << *this;
}
于 2011-07-17T05:20:20.620 に答える
0

オーバーロードされたストリーム演算子の宣言は次のようになります。

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

std::ostreamのオブジェクトへの参照を返す必要があります。これ&は、オーバーロードされた関数プロトタイプに誤って配置されています。

こちらの作業サンプルをご覧ください。

完全を期すために、ここにソースコードを追加します。
注:デモンストレーションを簡単にするために、クラスNodeのメンバーを公開しました。

#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;
}
于 2011-07-17T05:22:31.173 に答える