C++重载输出操作符<<遇到的问题
怪我咯
怪我咯 2017-04-17 14:29:02
0
1
810
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
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
Ty80

I can’t see your other code to fully understand the situation. Judging from the only fragments, the error is caused by overloading the operator of the global <<.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!