二进制表达式的操作数无效("常量字符*"和"常量字符*")
Invalid operands to binary expression ('const char*' and 'const char *')
我想知道这个错误的原因,请用简单的单词。
#include <iostream>
#include <string>
int main()
{
std::string exclam = "!";
std::string message = "Hello" + ", world" + exclam;
std::cout << message << std::endl;
return 0;
}
test.cpp:55:35:错误:二进制表达式的操作数无效("常量字符*"和"常量字符*"( std::字符串消息 = "Hello" + ", world" + exclam;
"Hello"
和", world"
不是字符串,它们是const char *
,对于+
运算符来说没有重载。
您必须执行以下操作:
std::string message = std::string("Hello") + std::string(", world") + exclam;
相关文章:
- 如何循环访问常量字符**?
- constexpr 函数获取常量字符*
- 如何计算常量字符**中的总字符数?
- QDial:如何将 int 值传递给需要常量字符*的 Qfile?
- 为什么 std::string_view 比常量字符*快?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 在编译时将常量字符* 转换为常量 char_type*
- 将字符串数组传递给接受常量字符**的函数
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 字符串强制转换为常量字符*
- 使用常量字符*时退出代码 139
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法<string>从"常量字符 []"转换为<类名>
- 初始化常量字符* 数组
- 常量字符*的性质是什么?
- C++ 获取向量中常量字符* [ ] 的长度
- 无法将参数 1 从 WCHAR 转换为常量字符 *
- 如何在 c++ 中将向量转换为<string>常量字符* const*?
- C++将常量字符* 指针数组传递给对象