Ky6000 Ky6000 - 3 months ago 17
C++ Question

Can't overload "<<" operator

Everywhere I look I see everyone overloading the << operator in a few lines, it seems very simple, but for some reason, overloading the << operator in my code does nothing.

In my .h I have:

friend std::ostream& operator<<(std::ostream& os, const Test& test);


And in my .cpp I have:

std::ostream& operator<<(std::ostream& out,const DeckOfCards& deck) {
out << "oi"; //just testing with a normal string before i try methods
return out;
}


And finally in the main function I have:

Test* test = new Test();
std::cout << "output is: " << test << std::endl;


Could someone please tell me what I'm doing wrong?
Thanks in advance.

Answer

How about trying this:

std::cout << "output is: " << *test << std::endl;

In your code, you are couting the pointer, not the object.