二进制表达式的操作数无效(' const char *'和' const char *')

时间:2018-04-09 02:41:31

标签: c++

我想知道容易出错的原因。

#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;

  

1 个答案:

答案 0 :(得分:6)

"Hello"", world"不是字符串,它们是const char *,对+运算符没有重载。

你必须做这样的事情:

std::string message = std::string("Hello") + std::string(", world") + exclam;