Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 549
Ky6000 Avatar asked Jan 20 '26 11:01

Ky6000


1 Answers

How about trying this:

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

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

like image 170
The Quantum Physicist Avatar answered Jan 22 '26 02:01

The Quantum Physicist