C++ 如何使用+运算符连接字符串
为什么一个允许而另一个产生错误。任何能解释的人C++ 如何使用+运算符连接字符串,c++,string,concatenation,C++,String,Concatenation,为什么一个允许而另一个产生错误。任何能解释的人 #include<string> using namespace std; int main() { string s3 = "Why"; string s11 = "hello" + " , " + s3; // It gives error string s11 = s3 + " , " +"hello" ; // This works fine. } hello是字符串文字,其类型为字符数组char[
#include<string>
using namespace std;
int main()
{
string s3 = "Why";
string s11 = "hello" + " , " + s3; // It gives error
string s11 = s3 + " , " +"hello" ; // This works fine.
}
hello是字符串文字,其类型为字符数组char[6]。运算符+仅为std::string定义。可以使用udl s将其设置为std::string:
hello是字符串文字,其类型为字符数组char[6]。运算符+仅为std::string定义。可以使用udl s将其设置为std::string:
由于运算符的优先级,该行
string s11 = "hello" + " , " + s3;
处理为
string s11 = ("hello" + " , " ) + s3;
string s11 = (s3 + " , ") + "hello"
子表达式hello+不合法。
第一项是char const[6]类型,一个6 char const的数组,第二项是char const[4]类型,一个4 char const的数组
两者之间没有+运算符。这就是为什么它是一个编译器错误
第二行
string s11 = s3 + " , " + "hello"
处理为
string s11 = ("hello" + " , " ) + s3;
string s11 = (s3 + " , ") + "hello"
子表达式s3+是有效的,因为支持该操作的运算符+重载。子表达式的计算结果为std::string。因此,后续的+hello也是一个受支持的操作。由于运算符的优先级,行
string s11 = "hello" + " , " + s3;
处理为
string s11 = ("hello" + " , " ) + s3;
string s11 = (s3 + " , ") + "hello"
子表达式hello+不合法。
第一项是char const[6]类型,一个6 char const的数组,第二项是char const[4]类型,一个4 char const的数组
两者之间没有+运算符。这就是为什么它是一个编译器错误
第二行
string s11 = s3 + " , " + "hello"
处理为
string s11 = ("hello" + " , " ) + s3;
string s11 = (s3 + " , ") + "hello"
子表达式s3+是有效的,因为支持该操作的运算符+重载。子表达式的计算结果为std::string。因此,随后的+hello也是一个受支持的操作。哦,太好了,它正在工作。你能解释一下你参加的UDL是什么吗,哦,太好了,它起作用了。你能解释一下你参加的UDL是什么吗,