Ky6000 Ky6000 - 1 year ago 97
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 Source

How about trying this:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download