AFraser AFraser - 3 months ago 24
C++ Question

Overloading C++ Insertion Operator (<<)

I'm trying to write a class that overloads the insertion operator but in my header file I get the error.

Overloaded 'operator<<' must be a binary operator (has 3 parameters)


Here is my code:

.h file

ostream & operator<<(ostream & os, Domino dom);


.cpp file

ostream & operator<< (ostream & os, Domino dom) {
return os << dom.toString();
}


I'm following a text book and this is what they use as an example but its not working for me.. Any suggestions?

Answer

You probably put your operator<< inside a class declaration. That means it takes an extra hidden parameter (the this parameter). You need to put it outside of any class declaration.

Comments