错误:类型为'const char [35]'和'const char [2]'的操作数到二进制'operator +' [英] Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’
问题描述
在文件顶部有
#define AGE42
后来在文件中我多次使用ID,包括一些看起来像
的行 1 std :: string name =Obama;
2 std :: string str =Hello+ name +you are+ AGE +years old!
3 str + =你觉得+ AGE +岁吗?
我得到错误:
错误:类型为'const char [35]'和'const char [2]'的二进制运算符+'的无效操作
在第3行。我做了一些研究,发现这是因为C ++如何处理不同的字符串,并能够通过将AGE更改为字符串(AGE)来解决它。但是,我不小心错过了一个实例,直到今天,并且想知道为什么编译器不抱怨,即使我还有一个实例,它只是年龄。
通过一些试验和错误,我发现我只需要在不连接在函数体中创建的另一个字符串的行上 string(AGE)
。
我的问题是在后台发生了什么C ++不喜欢连接字符串与预处理器放在那里的字符串,除非你也连接你定义的字符串
pre<
std :: string str =Hello+world; //坏了!
operator +
char *
。没有定义 operator +
需要两个 char *
(实际上,写一个)。因此,在我的编译器,这会产生一个不能添加两个指针的错误(你的显然是数组的短语,但它是同样的问题)。
请考虑这:
std :: string str =Hello+ std :: string(world); // ok
有 $
作为lhs和
作为rhs,所以现在每个人都很高兴。 std :: string
的运算符+
你可以将它扩展到一个连接链,你喜欢。它可以弄乱,但。例如:
std :: string str =Hello+there+ std :: string(world) ; // 不好!
这不起作用,因为您尝试 +
两个 char *
s之前lhs已转换为 std :: string
。但这很好:
std :: string str = std :: string(Hello)+there世界; // ok
因为一旦转换为 std :: string
,您可以根据需要添加 char *
多少<$ p $ c> +
如果这仍然令人困惑,可以添加一些方括号来突出显示关联规则,然后用它们的类型替换变量名:
((std :: string(Hello)+there)+world);
((string + char *)+ char *)
第一步是调用 string operator +(string,char *)
,它在标准库中定义。用它们的结果替换这两个操作数:
((string)+ char *)
这正是我们刚刚做的,而且还是合法的。但尝试相同的事情:
((char * + char *)+ string)
因为第一个操作尝试添加两个 char *
故事的道德:如果你想确定一个连接链将工作,只需确保前两个参数之一显式地是 std :: string
。
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
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+’"
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".
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.
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."
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).
Now consider this:
std::string str = "Hello " + std::string("world"); // ok
There is a definition of operator +
that takes a const char*
as the lhs and a std::string
as the rhs, so now everyone is happy.
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:
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.
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) + 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.
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
.
这篇关于错误:类型为'const char [35]'和'const char [2]'的操作数到二进制'operator +'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!