我想知道容易出错的原因。
#include <iostream>
#include <string>
int main()
{
std::string exclam = "!";
std::string message = "Hello" + ", world" + exclam;
std::cout << message << std::endl;
return 0;
}
test.cpp:55:35:错误:二进制表达式的操作数无效(&#39; const char *&#39;&#39; const char *&#39;) std :: string message =&#34;你好&#34; +&#34;,世界&#34; + exclam;
答案 0 :(得分:6)
"Hello"
和", world"
不是字符串,它们是const char *
,对+
运算符没有重载。
你必须做这样的事情:
std::string message = std::string("Hello") + std::string(", world") + exclam;