Can't add strings in C++

前端 未结 7 2020
陌清茗
陌清茗 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:45

    C++ doesn't do many of the automatic 'behind the scenes' conversations of other OO languages.

    As Doug said you need to do std::string("hello") + std::string(" world"), the language doesn't do this for you.

    However you can do

    std::cout << "hello" << "world" << exclam;
    

    Because std::cout knows how to print a const char[] as well as a string

提交回复
热议问题