Error:
no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'node')
#include <iostream>
using namespace std;
class Node
{
int data;
Node *next;
public:
Node(int data1)
{
data = data1;
next = nullptr;
}
};
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6};
Node *rootNode = new Node(arr[0]);
cout << rootNode;
return 0;
}
I'm expecting to get the linked list.
Why would you expect that? How do you think
operator<<will know how you want your customNodeclass to be printed out?The code you have shown compiles and runs, but the output is simply the memory address of the
Nodeobject, not thedatavalue it holds. That is because there is an existingoperator<<that prints raw pointers.For what you want, you have to tell the operator how you want a
Nodeto be printed. You do that by overloadingoperator<<, eg:Output:
Online Demo
If you want to print a whole list, you are best off wrapping the
Nodelist inside another class, and then overloadingoperator<<for that class, eg:Output:
Online Demo