字符串我wan't初始化靜態字符緩衝區在下面的代碼中的字符串str,但我收到以下錯誤:初始化字符緩衝區在C++
error: cannot convert ‘std::string’ to >‘char’ in initialization
,如果我用
static char buf[500] = str.c_str();
我得到以下錯誤:
error: invalid conversion from ‘const char*’ to ‘char*’
下面是我的代碼:
std::string str = "<Version="+version+" Ret=\"false\"/>";
static char buf[500] = str;
int len=strlen(buf);
buf[len]='\0';
INFO("Static Buffer :: "<<buf);
那麼,這是因爲猜測編程不起作用。 –
你的'buf [len] ='\ 0''是多餘的:根據定義,'len'是'buf'中已經存在的第一個''\ 0''的索引。你正在做的是用''\ 0''替換''\ 0''。 –