C ++ std :: stringstream运算符<<超载

时间:2012-01-11 18:01:01

标签: c++ overloading

我有以下课程(prototipe):

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

操作符的实现方式如下:

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

现在,我试图像这样使用它:

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

out << t;

VS给了我错误,说没有匹配&lt;&lt;运营商。我错了什么?

1 个答案:

答案 0 :(得分:14)

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

应该是

std::ostream & operator<< (std::ostream &out, Token const &t)