我想制作一个字符串" test /"但我无法在初始字符串后添加斜杠。知道原因和方法吗?
string imgpath="test";
strcat(imgpath,"/");
这是我迄今为止所尝试过的。 我得到了
Error 1 error C2664: 'strcat' : cannot convert parameter 1 from 'std::string' to 'char *'
另一个
imgpath="test"+"/";
Error 1 error C2110: '+' : cannot add two pointers
答案 0 :(得分:4)
使用std::string::operator+=()
代替strcat()
。
string imgpath="test";
imgpath += "/";
至于你的第二个例子
imgpath=std::string("test") +"/";
答案 1 :(得分:3)
strcat
用于附加到c-string。您应该使用string::append
或string::operator+=
:
imgpath.append("/");
imgpath += "/";
对于您的第二个问题:"asd"
是char *
,而不是std::string
。所以它没有一个有用的+
运算符。此代码应如下所示:
string x = string("asd") + "xyz";