Can't add strings in C++

前端 未结 7 1997
陌清茗
陌清茗 2020-12-24 14:13
#include 


int main()
{
    const std::string exclam = \"!\";
    const std::string message = \"Hello\" + \", world\" + exclam;
    std::cout <&l         


        
7条回答
  •  遥遥无期
    2020-12-24 14:58

    "Hello" + ", world"
    

    Since these are c-style strings, you cannot append them with +. You can append a std::string to a c-style string, but not 2 c-style strings this way, instead add a std::string() constructor around one of them to make a temporary, ie:

    "Hello" + std::string(", world")
    

提交回复
热议问题