I am trying to overload the << operator, but I get the following error:
error: ambiguous overload for ‘operator<<‘ in ‘std::cout << “Test “‘
..Followed by 5 billion other errors similar to:
c:\mingw\bin../lib/gcc/mingw32/4.5.2/include/c++/ostream:165:7: note:
candidates are: …
This comes up because I’m using cout in my main.cpp file.
Here is my code:
In BinTree.h:
template <typename T>
class BinTree{
...
friend std::ostream& operator<< <>(std::ostream&, const T&);
In BinTree.cpp:
template <typename T>
std::ostream& operator<< (std:: ostream& o, const T& value){
return o << value;
}
Thanks in advance for any help you can give.
Your function has the same signature than the one already defined. This is why the compiler moans about ambigous overload. Your function tries to define a function to stream everything to a ostream. This function already exists in the standards library.
What you perhaps want to do is write a function that defines how a BinTree is streamed (to everything). Please note that the stream type is templated. So if you chain the calls to the stream operator it streams the concrete type.