Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::stringstream operator<< overloading

I have the following class(prototipe):

class Token
{
public:
    //members, etc.
    friend std::stringstream& operator<< (std::stringstream &out, Token &t);
};

And the operator is implemented like this:

std::stringstream & operator<< (std::stringstream &out, Token &t)
{
    out << t.getValue(); //class public method
    return out;
}

Now, I'm trying to use it like this:

std::stringstream out;
Token t;
//initialization, etc.

out << t;

And VS gives me error, saying that there is no match for << operator. What am I wrong in?

like image 598
Dan Tumaykin Avatar asked Jan 11 '12 18:01

Dan Tumaykin


1 Answers

std::stringstream & operator<< (std::stringstream &out, Token &t)

should be

std::ostream & operator<< (std::ostream &out, Token const &t)
like image 58
Fred Foo Avatar answered Nov 15 '22 22:11

Fred Foo