In the last days I made a container class (forward list STL style) and I started to test it.
Everything went fine until I started to test the Iterator part.
When I try to use the output operator, even if it has been overloaded, the compiler gives me this error: undefined reference to `operator<<(std::ostream&, Container<int>::Node const&)'.
#include<forward_list>
#include<container.h>
using std::cout;
using std::endl;
int main()
{
Container<int> test;
test.Insert(1);
test.Insert(2);
Container<int>::Iterator it;
for(it = test.Begin(); it != test.End(); it++)
{
cout<< *it <<endl; // <-- Compiler saying "undefined reference to operator <<"
}
//My aim is to make the container to work like STL's one
std::forward_list<int> az;
az.push_front(2);
az.push_front(3);
std::forward_list<int>::iterator itl = az.begin();
for(; itl != az.end(); itl++)
cout<< *itl <<endl;
}
Honestly I can't really understand why the compiler is telling me that, since both the output operator were overloaded, both in container and in node.
Can someone please explain me what I am doing wrong?
Iterator::operator* should return info, not the Node itself. (Does forward_list return an enigmatic "node" object or the object that it stores in a node?)
Thank you very much dutch, fixing it in the way you said solved the problem!
I understood what you meant (your example was very clear) but there is still a thing that I can't grasp:
The returned object was a Node because I thought the compiler would have reasoned in this way:
1 2 3 4 5 6 7
for(it = test.Begin(); it != test.End(); it++)
{
cout<< *it <<endl; // dereferencing the Iterator object with operator* returns me a Node
// which will be handled by the overloaded output operator in the
// Node class. That operator will access the info field of the node
// and will output it in the ostream which will then be returned
}