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

后端 未结 6 536
野性不改
野性不改 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:15

    AGE is defined as "42" so the line:

    str += "Do you feel " + AGE + " years old?";
    

    is converted to:

    str += "Do you feel " + "42" + " years old?";
    

    Which isn't valid since "Do you feel " and "42" are both const char[]. To solve this, you can make one a std::string, or just remove the +:

    // 1.
    str += std::string("Do you feel ") + AGE + " years old?";
    
    // 2.
    str += "Do you feel " AGE " years old?";
    

提交回复
热议问题