C++ 错误:类型为“const char [35]”和“const char [2]”的无效操作数到二进制“operator+”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23936246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’
提问by janovak
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
稍后在文件中我多次使用 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?";
I get the error:
我收到错误:
"error: invalid operands of types ‘const char [35]' and ‘const char [2]' to binary ‘operator+'"
“错误:'const char [35]'和'const char [2]'类型的无效操作数到二进制'operator+'”
on line 3. I did some research and found it was because of how C++ was treating the different strings and was able to fix it by changing "AGE" to "string(AGE)." However, I accidentally missed one of the instances until today and was wondering why the compiler wasn't complaining even though I still had an instance where it was just "AGE".
在第 3 行。我做了一些研究,发现这是因为 C++ 如何处理不同的字符串,并且能够通过将“AGE”更改为“string(AGE)”来修复它。然而,直到今天我不小心错过了一个实例,并且想知道为什么编译器没有抱怨,即使我仍然有一个只是“AGE”的实例。
Through some trial and error I found that I only need string(AGE)
on lines where I don't concatenate another string that was created in the function body.
通过一些反复试验,我发现我只需要string(AGE)
在不连接在函数体中创建的另一个字符串的行上。
My questions is "what is going on in the background that C++ doesn't like concatenating a string with a string put there by the preprocessor unless you are also concatenating string that you defined in the function."
我的问题是“C++ 不喜欢将字符串与预处理器放在那里的字符串连接的背景是什么,除非您还连接在函数中定义的字符串。”
回答by dlf
Consider this:
考虑一下:
std::string str = "Hello " + "world"; // bad!
Both the rhs and the lhs for operator +
are char*
s. There is no definition of operator +
that takes two char*
s (in fact, the language doesn't permit you to write one). As a result, on my compiler this produces a "cannot add two pointers" error (yours apparently phrases things in terms of arrays, but it's the same problem).
rhs 和 lhs foroperator +
都是char*
s。没有定义operator +
需要两个char*
s(实际上,该语言不允许您编写一个)。结果,在我的编译器上,这会产生一个“无法添加两个指针”的错误(你的显然是用数组来表达的,但这是同样的问题)。
Now consider this:
现在考虑这个:
std::string str = "Hello " + std::string("world"); // ok
There isa definition of operator +
that takes a const char*
as the lhs and a std::string
as the rhs, so now everyone is happy.
有是的定义operator +
,需要一个const char*
作为LHS和std::string
作为RHS,所以现在大家都高兴。
You can extend this to as long a concatenation chain as you like. It can get messy, though. For example:
您可以根据需要将其扩展为连接链的长度。不过,它可能会变得混乱。例如:
std::string str = "Hello " + "there " + std::string("world"); // no good!
This doesn't work because you are trying to +
two char*
s before the lhs has been converted to std::string
. But this is fine:
这是因为你试图不起作用+
2个char*
的LHS已被转换为S前std::string
。但这很好:
std::string str = std::string("Hello ") + "there " + "world"; // ok
Because once you've converted to std::string
, you can +
as many additional char*
s as you want.
因为一旦转换为std::string
,您就可以+
根据需要添加任意数量的char*
s。
If that's still confusing, it may help to add some brackets to highlight the associativity rules and then replace the variable names with their types:
如果这仍然令人困惑,添加一些括号来突出关联规则可能会有所帮助,然后将变量名称替换为其类型:
((std::string("Hello ") + "there ") + "world");
((string + char*) + char*)
The first step is to call string operator+(string, char*)
, which is defined in the standard library. Replacing those two operands with their result gives:
第一步是调用string operator+(string, char*)
,它是在标准库中定义的。用它们的结果替换这两个操作数给出:
((string) + char*)
Which is exactly what we just did, and which is still legal. But try the same thing with:
这正是我们刚刚所做的,并且仍然是合法的。但是尝试同样的事情:
((char* + char*) + string)
And you're stuck, because the first operation tries to add two char*
s.
你被卡住了,因为第一个操作试图添加两个char*
s。
Moral of the story: If you want to be sure a concatenation chain will work, just make sure one of the first two arguments is explicitly of type std::string
.
这个故事的寓意:如果您想确保串联链能够正常工作,只需确保前两个参数之一明确属于 type std::string
。
回答by clcto
AGE
is defined as "42"
so the line:
AGE
定义如下"42"
:
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 +
:
这是无效的,因为"Do you feel "
和"42"
都是const char[]
. 要解决此问题,您可以创建一个 a std::string
,或者只是删除+
:
// 1.
str += std::string("Do you feel ") + AGE + " years old?";
// 2.
str += "Do you feel " AGE " years old?";
回答by Paul Roub
In line 2, there's a std::string
involved (name
). There are operations defined for char[] + std::string
, std::string + char[]
, etc. "Hello " + name
gives a std::string
, which is added to " you are "
, giving another string, etc.
在第 2 行中,有一个std::string
涉及 ( name
)。有为char[] + std::string
、std::string + char[]
等定义的操作。"Hello " + name
给出一个std::string
,它被添加到" you are "
,给出另一个字符串等。
In line 3, you're saying
在第 3 行,你说
char[] + char[] + char[]
and you can't just add arrays to each other.
你不能只是互相添加数组。
回答by Christian Hackl
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).
您不能像这样连接原始字符串。operator+
仅适用于两个std::string
对象或一个std::string
和一个原始字符串(在操作的任一侧)。
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:
最简单的解决方案是将原始字符串转换为std::string
第一个:
"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
.
当然,您首先不应该使用宏。C++ 不是 C。const
在 C++11 中使用或在具有适当编译器支持的情况下使用constexpr
.
回答by djulien
In this particular case, an even simpler fix would be to just get rid of the "+" all together because AGE is a string literal and what comes before and after are also string literals. You could write line 3 as:
在这种特殊情况下,更简单的解决方法是将“+”一起去掉,因为 AGE 是字符串文字,前后也是字符串文字。您可以将第 3 行写为:
str += "Do you feel " AGE " years old?";
str += "Do you feel " AGE " years old?";
This is because most C/C++ compilers will concatenate string literals automatically. The above is equivalent to:
这是因为大多数 C/C++ 编译器会自动连接字符串文字。以上相当于:
str += "Do you feel " "42" " years old?";
str += "Do you feel " "42" " years old?";
which the compiler will convert to:
编译器将转换为:
str += "Do you feel 42 years old?";
str += "Do you feel 42 years old?";
回答by Dig The Code
I had the same problem in my code. I was concatenating a string to create a string. Below is the part of code.
我在我的代码中遇到了同样的问题。我正在连接一个字符串来创建一个字符串。下面是部分代码。
int scannerId = 1;
std:strring testValue;
strInXml = std::string(std::string("<inArgs>" \
"<scannerID>" + scannerId) + std::string("</scannerID>" \
"<cmdArgs>" \
"<arg-string>" + testValue) + "</arg-string>" \
"<arg-bool>FALSE</arg-bool>" \
"<arg-bool>FALSE</arg-bool>" \
"</cmdArgs>"\
"</inArgs>");