gcc与字符串类型不一致 [英] gcc inconsistent about string type
本文介绍了gcc与字符串类型不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下测试程序:
#include <string>
#include <iostream>
int main()
{
std::string s;
std::string a = "sd";
std::cout << a==s ? "y" : "n";
return 0;
}
尝试使用 g ++ test.cpp
给出以下隐含错误:
Trying to compile this with g++ test.cpp
gives the following cryptic error:
error: no match for 'operator==' (operand types are 'std::basic_ostream<char>' and 'std::string {aka std::basic_string<char>}')
std::cout << a==s ? "y" : "n";
^
看起来 s
正在正确编译为 std :: string
,而 a
正在编译为 std :: basic_ostream< char>
!?帮助!!
It seems that s
is being correctly compiled as type std::string
, while a
is being compiled as std::basic_ostream<char>
!? HELP!!
推荐答案
编译器将您的语句解析为((std :: cout< a)== s)? y:n;
因为运算符优先< a>:您需要括号。
The compiler parsed your statement as ((std::cout << a) == s) ? "y" : "n";
because of operators precedence : You need parentheses.
std::cout << (a==s ? "y" : "n");
这篇关于gcc与字符串类型不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文