错误:类型'const char [35]'和'const char [2]'到二进制'operator +'的操作数无效

jan*_*vak 51 c++ string stdstring c-preprocessor

在我的文件的顶部,我有

#define AGE "42"
Run Code Online (Sandbox Code Playgroud)

稍后在文件中我多次使用ID,包括一些看起来像的行

1 std::string name = "Obama";
2 std::string str = "Hello " + name + " you are " + AGE + " years old!";
3 str += "Do you feel " + AGE + " years old?";
Run Code Online (Sandbox Code Playgroud)

我收到错误:

"错误:类型'const char [35]'和'const char [2]'到二进制'运算符+''的操作数无效"

第3行.我做了一些研究,发现这是因为C++如何处理不同的字符串,并且能够通过将"AGE"更改为"string(AGE)"来修复它.但是,直到今天我才意外地错过了其中一个实例,并且想知道为什么编译器没有抱怨,即使我还有一个只是"AGE"的实例.

通过一些反复试验,我发现我只需要string(AGE)在不连接函数体中创建的另一个字符串的行上.

我的问题是"在后台发生的事情是C++不喜欢将字符串与预处理器放置的字符串连接起来,除非你还连接了你在函数中定义的字符串."

dlf*_*dlf 83

考虑一下:

std::string str = "Hello " + "world"; // bad!
Run Code Online (Sandbox Code Playgroud)

rhs和lhs operator +都是char*s.没有定义operator +需要两个char*(事实上​​,语言不允许你写一个).结果,在我的编译器上产生了一个"无法添加两个指针"的错误(你的显然是根据数组的短语,但它是同样的问题).

现在考虑一下:

std::string str = "Hello " + std::string("world"); // ok
Run Code Online (Sandbox Code Playgroud)

的定义operator +,需要一个const char*作为LHS和std::string作为RHS,所以现在大家都高兴.

您可以将此扩展到您喜欢的连接链.但它可能会变得混乱.例如:

std::string str = "Hello " + "there " + std::string("world"); // no good!
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为你在lhs转换为之前尝试了+两次.但这很好:char*std::string

std::string str = std::string("Hello ") + "there " + "world"; // ok
Run Code Online (Sandbox Code Playgroud)

因为一旦你转换成了std::string,你可以+根据需要添加更多的char*s.

如果这仍然令人困惑,可能有助于添加一些括号以突出显示关联性规则,然后将变量名称替换为其类型:

((std::string("Hello ") + "there ") + "world");
((string + char*) + char*)
Run Code Online (Sandbox Code Playgroud)

第一步是调用string operator+(string, char*),它在标准库中定义.用它们的结果替换这两个操作数给出:

((string) + char*)
Run Code Online (Sandbox Code Playgroud)

这正是我们刚刚做的,哪些仍然合法.但尝试相同的事情:

((char* + char*) + string)
Run Code Online (Sandbox Code Playgroud)

你被卡住了,因为第一次操作试图添加两个char*s.

故事的道德:如果你想确定串联链是否有效,只需确保前两个参数之一明确是类型std::string.


clc*_*cto 11

AGE被定义为"42"如此行:

str += "Do you feel " + AGE + " years old?";
Run Code Online (Sandbox Code Playgroud)

转换为:

str += "Do you feel " + "42" + " years old?";
Run Code Online (Sandbox Code Playgroud)

因为这是无效的"Do you feel ",并"42"const char[].要解决这个问题,你可以制作一个std::string,或者只删除+:

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

// 2.
str += "Do you feel " AGE " years old?";
Run Code Online (Sandbox Code Playgroud)


Pau*_*oub 5

在第 2 行中,有一个std::string涉及 ( name)。有为char[] + std::stringstd::string + char[]等定义的操作。"Hello " + name给出一个std::string,它被添加到" you are ",给出另一个字符串等。

在第 3 行,你说

char[] + char[] + char[]
Run Code Online (Sandbox Code Playgroud)

你不能只是互相添加数组。


Chr*_*ckl 5

您不能像这样连接原始字符串。operator+仅适用于两个std::string对象或一个std::string和一个原始字符串(在操作的任一侧)。

std::string s("...");
s + s; // OK
s + "x"; // OK
"x" + s; // OK
"x" + "x" // error
Run Code Online (Sandbox Code Playgroud)

最简单的解决方案是将原始字符串转换为std::string第一个:

"Do you feel " + std::string(AGE) + " years old?";
Run Code Online (Sandbox Code Playgroud)

当然,您首先不应该使用宏。C++ 不是 C。const在 C++11 中使用或在具有适当编译器支持的情况下使用constexpr.