可能重复:
C++ deprecated conversion from string constant to ‘char*’警告:从字符串常量“字符*”不赞成使用转换
我一直在寻找进入C字符串++,并试图练习实验的一些行为在字符串库中定义的函数。我昨天编译了同一个程序,所有的工作都完全没有警告或错误。但是,今天我试图再次编译该程序,但我收到以下警告。
D:\C++ CodeBlocks Ex\Strings\main.cpp||In function 'int main()':|
D:\C++ CodeBlocks Ex\Strings\main.cpp|11|warning: deprecated conversion from string constant to 'char*'|
||=== Build finished: 0 errors, 1 warnings ===|
该警告反映了该行strncat("Hello",string,STRMAX-strlen(string));
。我不确定,但是从我怀疑的是,strncat函数不喜欢将文字数组连接到字符串常量的想法。任何帮助,将不胜感激。
#include <iostream>
#include <string.h>
using namespace std;
int main(){
#define STRMAX 599
char string[STRMAX+1];
cout<<"Enter name: ";
cin.getline(string,STRMAX);
strncat("Hello",string,STRMAX-strlen(string));
cout<<string;
return 0;
}
我认为更大的问题在于你使用'strncat()'完全不正确。 –
你也应该使用std :: string而不是char数组。 – Borgleader
您使用的是C字符串,而不是C++字符串。 C++的字符串类及其方法位于标题中,而不是(通常在C++中用编写)。 –
sepp2k