无法在C ++中添加字符串 [英] Can't add strings in C++

查看:112
本文介绍了无法在C ++中添加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>


int main()
{
    const std::string exclam = "!";
    const std::string message = "Hello" + ", world" + exclam;
    std::cout << message;
    return 0;
}

为什么此代码不起作用?返回错误:

Why does this code not work? Error returned:

error: invalid operands of types `const char[6]' and `const char[8]' to binary `operator+'

谢谢!

编辑:

感谢所有答案。这是我第一次访问该网站,在如此短的时间间隔内,详尽的解释使我惊讶。

Thanks for all the answers. This is my first time on the site and I am astonished at the number of elaborate explanations in such a short time interval.

关于实际问题。

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

是因为,世界,然后是!会与变量hello(已定义)连接?

Is it because ", world" and afterwards "!" get concatenated with variable hello (which is defined)?

推荐答案

因为在C ++中是字符串文字(例如 Hello 的类型不是 std :: string 。它们是纯字符数组或C风格的字符串。

Because in C++, string literals (like "Hello" are not of type std::string. They are plain char arrays, or C-style strings.

因此对于行 const std :: string message = Hello +,world + exclam; ,编译器具有的类型

So for the line const std::string message = "Hello" + ", world" + exclam;,the types the compiler has to work with are:

const std :: string message = const char [6] + const char [8] + std :: string;

并给出 + 的关联性,它必须执行的操作是:

and given the associativity of +, the operations it has to perform are:

const std :: string message =((const char [6] + const char [8])+ std :: string);

也就是说,必须首先评估最左边的加法,然后将结果传递到最右边的加法。

That is, the left-most addition must be evaluated first, and the result passed to the rightmost addition.

因此,编译器尝试评估 const char [6] + const char [8]
没有为数组定义加法。数组是隐式的ly转换为指针,但这对编译器没有帮助。这只是意味着它以 const char * + const char * 结尾,并且也没有为指针定义任何附加内容。

So the compiler tries to evaluate const char[6] + const char[8]. There is no addition defined for arrays. Arrays are implicitly converted to pointers, but this doesn't help the compiler. That just means it ends up with const char* + const char*, and no addition is defined for pointers either.

在这一点上,它不知道您是否希望将结果转换为 std :: string

At this point, it doesn't know that you want the result to be converted to a std::string.

但是,在第二个示例中:

However, in your second example:

const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";

它可以工作,因为编译器看到的操作是 std :: string + const char [8] + const char [2] 。在这里,第一个加法可以转换为 std :: string + const char * ,在这里定义了加法运算符 ,并返回 std :: string 。因此,编译器已成功找出第一个加法,并且由于结果是字符串,所以第二个加法如下: std :: string + const char [2] ,就像以前一样,这是不可能的,但是可以将数组转换为指针,然后编译器可以找到一个有效的加法运算符,再次导致 std :: string

it works, because the operations the compiler would see were std::string + const char[8] + const char[2]. Here, the first addition can be converted to std::string + const char*, and here the addition operator is defined, and returns a std::string. So the compiler has successfully figured out the first addition, and since the result was a string, the second addition looks like this: std::string + const char[2], and like before, this isn't possible, but the array can be converted to a pointer, and then the compiler is able to find an addition operator that works, again resulting in a std::string.

这篇关于无法在C ++中添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆