preface
When using strings in C++, we habitually use +
to connect two strings enclosed in ""
. The error is reported: error: invalid operators of types' const char [6] 'and' const char [6] 'to binary' operator+'
,
1. Scenarios
//std::string str = "hello" + "world"; // error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'
//std::string str = std::string("hello") + " world"; // correct
//std::string str = "hello" + std::string("world"); //correct
std::string str = "hello"" world"; //correct
cout << "str=" << str << endl;
When using the+operator to connect two strings wrapped with ""
, an error occurs. The reason is that in C++, the strings enclosed by ""
are regarded as const char* types
, rather than string types.
2. Solution
Refer to the explanation on stackoverflow. For details, please read error: invalid operators of types’ const char * ‘and’ const char * ‘to binary’ operator+’
1. Explicitly declare one of them as std::string type (recommended)
std::string str = std::string("hello") + " world";
2. Remove the+and let the compiler splice strings (not recommended)
std::string str = "hello"" world";
Most compilers automatically splice strings enclosed by ""
, but it is not guaranteed that all compilers are normal. It is recommended to display strings declared as string types and then perform the+operation.
3. Validation
std::string str1 = std::string("hello") + " world" + "!"; // correct
std::string str2 = "hello" + std::string(" world") + "!"; //correct
std::string str3 = "hello"" world" "!"; //correct
cout << "str1=" << str1 << endl;
cout << "str2=" << str2 << endl;
cout << "str3=" << str3 << endl;
Read More:
- [Solved] C++ Error: terminate called after throwing an instance of ‘char const‘
- [Solved] Error: In the Function of ‘fmt::v8::detail::error_handler::on_error(char const*)’
- [Error] invalid operands to binary ^ (have ‘double‘ and ‘float‘)
- C++ Compile Error: error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]
- Sending ‘const NSString *’ to parameter of type ‘NSString *’ discards qualifiers [Solved]
- [Solved] sending const NSString * to parameter of type NSString * discards qualifiers
- Tensorflow C++:You must define TF_LIB_GTL_ALIGNED_CHAR_ARRAY for your compiler
- [Solved] ERROR_POLICY: attempt to perform an operation not allowed by the security policy `PDF‘ @ error/const
- [Solved] error: taking address of temporary array av_make_error_string((char[AV_ERROR_MAX_STRING_SI…
- CIBERSOFT $operator is invalid for atomic vectors [How to Solve]
- R language learning problem solving error in output $nodeid: $operator is invalid for atomic vectors
- No match for ‘operator =’ both ends of the equal sign do not match
- RuntimeError: Exporting the operator uniform to ONNX opset version 12 is not supported.
- [Solved] Matlab Code generate error: failed to generate all binary outputs
- [Solved] MindSpore Error: StridedSlice operator does not support input of uint8 data type on Ascend hardware
- [Solved] GLSL Lint: Failed to spawn ‘glslangValidator‘ binary. Error: spawn glslangValidator.exe ENOENT
- C Language error: two or more data types in declaration specifiers
- C++ new types may not be defined in a return type Error?
- [Solved] SyntaxError: Invalid regular expression: invalid group specifier name