我知道这是一个普遍的问题,但是寻找参考文献和其他材料,我对此问题没有明确的答案。

考虑以下代码:

#include <string>

// ...
// in a method
std::string a = "Hello ";
std::string b = "World";
std::string c = a + b;

编译器告诉我,它找不到一个超载的操作员 char[dim].

这是否意味着在字符串中没有 +运算符?

但是在几个示例中,有这样的情况。如果这不是正确的con依的正确方法,那么最好的方法是什么?

有帮助吗?

解决方案

您的代码(如书面)可行。您可能正在尝试实现无关的东西,但类似:

std::string c = "hello" + "world";

这无效,因为对于C ++,这似乎您正在尝试添加两个 char 指针。相反,你 需要 至少转换一个 char* 文字 std::string. 。您可以做问题中已经发布的事情(正如我说的那样,此代码 将要 工作)或您进行以下操作:

std::string c = std::string("hello") + "world";

其他提示

std::string a = "Hello ";
a += "World";

我会这样做:

std::string a("Hello ");
std::string b("World");
std::string c = a + b;

在VS2008中编译。

std::string a = "Hello ";
std::string b = "World ";
std::string c = a;
c.append(b);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top