0

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;
}
Jason A.
  • 71
  • 1
  • 6
  • So it's not possible to do it from this standpoint? Is it best to simply call out each of them in turn from the node function itself? – Jason A. Jul 17 '11 at 05:36

2 Answers2

0

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 &currentNode)
{
    // Output values here.
    return output;
}

void Node::nodeFunction()
{
     cout << *this;
}
Sven
  • 21,903
  • 4
  • 56
  • 63
  • Wish I could use references, but I don't have a choice. I really appreciate the code, but the same errors came up when I tried calling to the items using the currentNode parameter. – Jason A. Jul 17 '11 at 05:27
  • passing âconst Nodeâ as âthisâ argument of âstd::string Node::getFirstName()â discards qualifiers – Jason A. Jul 17 '11 at 05:38
  • I tried using currentNode->getFirstName() within the operator – Jason A. Jul 17 '11 at 05:39
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;
}
Alok Save
  • 202,538
  • 53
  • 430
  • 533