2011-07-17 3 views
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; 
} 
+0

このような観点からはできません。ノード関数自体から各ノードを順番に呼び出すのが最善でしょうか? –

答えて

0

あなたのオーバーロードされたストリーム演算子の宣言すべきこのようなこと:

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

あなたは0、std::ostreamのオブジェクトへの参照を返すべきですが、オーバーロードされた関数プロトタイプに間違って配置されています。

hereをご覧ください。

ソースコードをここに追加してください。
注:私は簡単にデモンストレーションのためにクラス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; 
} 
+0

私は名前空間stdを使用していますが、それをクリアしていますか?申し訳ありませんが、私はコードで言及しませんでした。 –

+0

@Jason A.:編集された回答を参照して、あなたが達成しようとしているものの編集可能な作業コピーを追加しました。 –

+0

うん。どうもありがとう!ほんとうにありがとう! –

0

演算子の戻り値の&は間違った場所にあり、そしてそれはostreamに事業者のための参照ではなく、ポインタを使用することが一般的に良いでしょう:

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

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

私は参考文献を使うことができましたが、私は選択肢がありません。私はコードを本当に感謝しますが、currentNodeパラメータを使って項目を呼び出そうとしたときに同じエラーが発生しました。 –

+0

実際のエラーメッセージを投稿した場合は役に立ちます。 – Sven

+0

âconstNodeâを渡してâstst:: string Node :: getFirstName()の引数が修飾子を捨てます –