Consider the following code :
class TextMessage{
public :
    TextMessage(){};
    TextMessage(std::string _text):text(_text){}
    std::string text;
    friend std::ostream & operator<<( std::ostream & os, const TextMessage & m);
};
std::ostream & operator<<( std::ostream & os, const TextMessage & m){
    return os << "text message : " << m.text;
}
Why on earth :
<<
std::cout << textMsgInstance; crashes by stackoverflow as predicted by Visual ?Btw, replacing m.text by m.text.c_str() works.
I'm guessing that you failed to #include <string>. Thus, when the compiler comes to output a std::string, it can't, and starts looking for implicit conversions- and your implicit constructor to a TextMessage looks like just the bill. But wait- now we're outputting a TextMessage in the TextMessage's output function, and bam.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With