>  Q&A  >  본문

C++重载输出操作符<<遇到的问题

std::ostream& operator<<(std::ostream& os, const Token& token){
    os << 1;
    return os;
}

报错信息是这样的 :

/Users/zhangzhimin/ClionProjects/Fred/Font/Lexer/Token.cpp:33:8: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'int')
    os << 1;
    ~~ ^  ~
/Users/zhangzhimin/ClionProjects/Fred/Font/Lexer/Token.cpp:32:15: note: candidate function not viable: no known conversion from 'int' to 'const Token' for 2nd argument
std::ostream& operator<<(std::ostream& os, const Token& token){
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5382:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-2>' against 'int'
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:4151:1: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against 'int'
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
1 error generated.
make[2]: *** [CMakeFiles/Fred.dir/Font/Lexer/Token.cpp.o] Error 1
make[1]: *** [CMakeFiles/Fred.dir/all] Error 2
make: *** [all] Error 2
怪我咯怪我咯2765일 전816

모든 응답(1)나는 대답할 것이다

  • PHP中文网

    PHP中文网2017-04-17 14:31:02

    看不到你其他代码不能全面的了解情况,从仅有的片段来看是由重载了全局<<运算符导致的错误。

    회신하다
    0
  • 취소회신하다