简体   繁体   中英

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.

How about trying this:

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

In your code, you are cout ing the pointer, not the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM