向字符串添加正斜杠

Geo*_*iuc 3 c++ string

我想创建一个字符串“test/”,但我无法在初始字符串后添加斜杠。知道为什么以及如何吗?

string imgpath="test";
strcat(imgpath,"/");
Run Code Online (Sandbox Code Playgroud)

这是我迄今为止尝试过的。我得到

Error   1   error C2664: 'strcat' : cannot convert parameter 1 from 'std::string' to 'char *'
Run Code Online (Sandbox Code Playgroud)

而另一个

imgpath="test"+"/";

Error   1   error C2110: '+' : cannot add two pointers
Run Code Online (Sandbox Code Playgroud)

πάν*_*ῥεῖ 5

使用std::string::operator+=()代替strcat()

string imgpath="test";
imgpath += "/";
Run Code Online (Sandbox Code Playgroud)

至于你的第二个例子

imgpath=std::string("test") +"/";
Run Code Online (Sandbox Code Playgroud)