C++ std::stringstream operator<< 重载

标签 c++ overloading

我有以下类(class)(原型(prototype)):

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 给我报错,说 << 运算符不匹配。我做错了什么?

最佳答案

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

应该是

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

关于C++ std::stringstream operator<< 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824280/

相关文章:

c++ - 使用模板重载功能

Python 重载原语

c++ - 可变参数模板的显式模板实例化

c++ - 变量周围的调试错误堆栈已损坏

c++ - C++ 中的 DBL_MAX 是什么?

c++ - C++中的多线程链表

c++ - 重载算术运算符 C++

c++ - 错误 C2228 : left of '.values' must have class/struct/union

c++ - 线程与 pthread 条件变量同步

c++ - 当您调用方法/函数时,汇编语言会发生什么?