Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

后端 未结 6 541
野性不改
野性不改 2020-11-28 20:36

At the top of my file I have

#define AGE \"42\"

Later in the file I use ID multiple times including some lines that look like



        
6条回答
  •  一个人的身影
    2020-11-28 21:14

    You cannot concatenate raw strings like this. operator+ only works with two std::string objects or with one std::string and one raw string (on either side of the operation).

    std::string s("...");
    s + s; // OK
    s + "x"; // OK
    "x" + s; // OK
    "x" + "x" // error
    

    The easiest solution is to turn your raw string into a std::string first:

    "Do you feel " + std::string(AGE) + " years old?";
    

    Of course, you should not use a macro in the first place. C++ is not C. Use const or, in C++11 with proper compiler support, constexpr.

提交回复
热议问题