0

I am trying print new created node with dereferencing operator.

My main function

int main ()
{
    Insert(3);
    return 0;
}

Insert() function

void Insert(int data)
{
    Node *temp = new Node ;
    temp -> data = data ;
    temp -> next = NULL ;

    cout << *temp ;

}

My error :

tp.cpp: In function ‘void Insert(int)’:
tp.cpp:27:10: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘Node’)
     cout << *temp ;
Vivek Iyer
  • 154
  • 1
  • 6
  • 16
  • 1
    There is no `<<` overload for `Node` in the standard library. You also leak `Node *temp = new Node ;` – user4581301 Aug 02 '17 at 03:37
  • Then how to see node's value then that i just declared ? Thanks ! – Vivek Iyer Aug 02 '17 at 03:39
  • 1
    You will have to write your own `<<` operator to handle `Node`. Look up the Stream (Bitshift) Operators here: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – user4581301 Aug 02 '17 at 03:42

2 Answers2

1

Problem: C++ provides operators for its basic types only, for user defined types we have to write our own operator. The error no match for ‘operator<<’ says that the compiler couldn't find a << operator against the statement cout << *temp ;

Possible solutions:

  1. Like @user4581301 said, You can write your own operator of << for your type Node.

  2. You can also replace the cout << *temp ; statement with cout << temp->data ; or cout << (*temp).data ; since temp is the pointer to a structure. you can either access it with and -> before the data or with a . but after dereferencing it using the * operator.

Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
0

Any operator is basically a functions. For instance, an addition operator overloaded operator + for your class Rational might be expressed as Member function:

class Rational
{
  private:
    ...
    ...
  public:
    ...
    //Implicit arg "this" is Left Hand operand.
    //Explicitly stated arg objRational is Right Hand operand.
    Rational operator+(const Rational& objRational) const {...};
    ...
};

Please note, that the argument is made const here to ensure we do not accidentally modify the operand itself, and also the function marks itself const to make sure we don't modify this object. Arg is passed by ref because if we are not modifying it for sure, no need to copy and no harm in ref.

The above is technically a function, so you can do something like

Rational r1(3, 4), r2(22, 7);
Rational r3 = r1.operator+(r2); //Really possible! Try it!

It's then just a syntactic sugar added to C++ grammar to allow the same call as

Rational r3 = r1 + r2;


You wrote

cout << *temp ;

Of which, type of (*temp) is Node, and cout is an object of ostream class.

So, the compiler is now looking for an overloaded operator with left hand operand an ostream object, and right hand operand a Node object.

So, it is as good as writing, somewhere inside the class ostream itself,

class ostream : ...
{
  ...
  ...
  //Here, the only one explicit arg is Node, 
  //which is Right Hand side operand.
  //Since it is a member operator, 
  //implicit arg "this" will be, by default, the LH operand.
  public ostream& oprtator<<(const Node& objNode){...}
  ...
}

But, we do not have that possibility, since we did not write class ostream in the first place. And when it was written, your Node did not exist! Also, it may need to access private members of objNode which will not be allowed for a function which isn't member of Node class.

If we try to put it as a member of class Node, it can access Node's private members. But now the left hand operand of the operator<< will need to be Node, which ruins the whole purpose.

So, what we do is making it a friend function.

class Node
{
    ...
    ...
  public:
    ...
    //Since this is NOT a member, bot LH and RH operand need to be stated explicitelty
    friend ostream& operator<< (ostream& out, const Node& objNode) { out << node.data; return out; }
    ...
};

This creates an operator<< which has left operand an ostream and right operand a node, and which can access private members of Node because it's friend. (^_^)

Why accept and return reference of ostream object?

Because, when you try to chain the calls, you need the same object serially getting written into.

Consider a statement: cout << "First: " << objNode1 << ", Second: " << objNode2;

It will be evaluated as

(
  (
    (
      cout << "First: " //Innermost call
    ) << objNode1       
  ) << ", Second: " 
)<< objNode2;

From inside out, for each call, we need the ostream objects to be passed to the overloaded operator which has all the previous insertions, and therefore the inner calls need to return the reference to modified (after insertion) object of ostream to be returned.

Hope it helps; Happy Coding :)

Abhay Bhave
  • 44
  • 1
  • 8