gpt4 book ai didi

c++ - 在 C++ 中使用 "+"连接字符串

转载 作者:太空狗 更新时间:2023-10-29 23:36:22 24 4
gpt4 key购买 nike

<分区>

我是 C++ 的新手,我指的是 Accelerated C++。在尝试其中一个练习题时,上面写着:

Are the following definitions valid? Why or why not?
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;

当我尝试并执行该程序时,出现如下错误:

invalid operands of types to binary operator +.

但是下面的代码工作得很好:

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

我不清楚它的执行情况!为什么这种连接在第一种情况下不起作用?

谢谢!我正在使用 DEV C++。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com