C++ 错误 C2220:警告被视为错误 - 未生成“对象”文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18225636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2220: warning treated as error - no 'object' file generated
提问by TrungLuu
I have below class
我有以下课程
class Cdata12Mnt
{
public:
char IOBname[ID1_IOB_PIOTSUP-ID1_IOB_TOP][BOADNAM_MAX + 4];
char ExIOBname[ID1_MAX_INF-ID1_EXIOB_U1TOP][BOADNAM_MAX + 4];
char cflpath[256];
char basetext[256];
UINT database[ID1_MAX_INF];
int State;
public:
char SelectPath[256];
public:
int GetIOBName(int slt,char *Name);
Cdata12Mnt(char *SelectPath);
virtual ~Cdata12Mnt();
int GetValue(int id);
int GetState() { return State; }
};
And I have function as below
我的功能如下
Cdata12Mnt::Cdata12Mnt(char *SelectPath)
{
SCTReg reg;
char buf[256], *cpnt, *npnt, *bpnt1, *bpnt2;
char *startcode[] = {"CNTL_CODE ","SEGMENT "};
char *stopcode = {"END_CNTL_CODE "};
FILE *fp;
int ii, infl;
State = 0;
for (ii = 0; ii < (ID1_IOB_PIOTSUP - ID1_IOB_TOP); ii++) {
strcpy(IOBname[ii], "");
}
for (ii = 0; ii < (ID1_MAX_INF-ID1_EXIOB_U1TOP); ii++) {
**strcpy(ExIOBname[ii], "");**
}
sprintf(cflpath, "%s\%s", SelectPath, CDATAFL);
if ((fp = fopen(cflpath,"r"))!=NULL) {
for (ii = 0, infl = 0; fgets(buf, 256, fp) != NULL;) {
if (infl == 0 && strncmp(buf, startcode[0], strlen(startcode[0])) == 0) {
if ((cpnt = strchr(&buf[strlen(startcode[0])],*startcode[1])) != NULL) {
if (strncmp(cpnt,startcode[1], strlen(startcode[1])) == 0) {
infl = 1;
continue;
}
}
}
if (infl == 0) {
continue;
}
if (strncmp(buf,stopcode,strlen(stopcode))==0) {
if (ii == ID1_EXIOB_U1TOP) {
for (int nDataNumber = ii; nDataNumber < ID1_MAX_INF; nDataNumber++) {
database[nDataNumber] = 0;
}
}
infl = 0;
continue;
}
if (strncmp(&buf[14], " DD ", 4) == 0) {
if ((cpnt=strchr(buf, ';')) != NULL) {
*cpnt = '// pragma_warning.cpp
// compile with: /W1
#pragma warning(disable:4700)
void Test() {
int x;
int y = x; // no C4700 here
#pragma warning(default:4700) // C4700 enabled after Test ends
}
int main() {
int x;
int y = x; // C4700
}
';
}
if (ii >= ID1_IOB_TOP && ii < ID1_IOB_PIOTSUP) {
if ((bpnt1 = strchr(cpnt + 1,'(')) != NULL && (bpnt2=strchr(cpnt + 1,')'))!=NULL && bpnt1 < bpnt2) {
*bpnt2 = '##代码##';
*(bpnt1 + BOADNAM_MAX + 1) = '##代码##';
strcpy(IOBname[ii-ID1_IOB_TOP], bpnt1 + 1);
}
}
if (ii >= ID1_EXIOB_U1TOP && ii < ID1_MAX_INF) {
if ((bpnt1 = strchr(cpnt + 1, '(')) != NULL && (bpnt2=strchr(cpnt+1,')'))!=NULL && bpnt1 < bpnt2) {
*bpnt2='##代码##';
*(bpnt1+BOADNAM_MAX+1)='##代码##';
strcpy(ExIOBname[ii-ID1_EXIOB_U1TOP], bpnt1 + 1);
}
}
for (cpnt = &buf[18]; cpnt != NULL;) {
if ((npnt=strchr(cpnt, ',')) != NULL)
*npnt='##代码##';
}
if (strchr(cpnt,'H')!=NULL) {
sscanf(cpnt,"%XH",&database[ii]);
} else {
database[ii]=atoi(cpnt);
}
ii++;
cpnt = npnt;
if (cpnt != NULL) {
cpnt++;
}
}
}
}
fclose(fp);
} else {
State=-1;
}
When I compile this function in Visual studio 2008, it gives me error at strcpy(IOBname[ii],"");
as below
当我在 Visual Studio 2008 中编译这个函数时,它给了我strcpy(IOBname[ii],"");
如下错误
error C2220: warning treated as error - no 'object' file generated
错误 C2220:警告被视为错误 - 未生成“对象”文件
How to fix this error?
如何修复此错误?
回答by Nym Anno
The error says that a warning was treated as an error. Therefore your problem is a warning message! Check them and fix these.
该错误表示警告被视为错误。因此,您的问题是一条警告消息!检查它们并修复它们。
In case you don't know how to find them: Open the Error List
(View
> Error List
) and click on Warning
.
如果您不知道如何找到它们:打开Error List
( View
> Error List
) 并单击Warning
。
回答by mojtaba setoodeh
Go to project properties -> configurations properties -> C/C++ -> treats warning as error -> No (/WX-)
.
去project properties -> configurations properties -> C/C++ -> treats warning as error -> No (/WX-)
。
回答by Xocoatzin
回答by zar
This error message is very confusing. I just fixed the other 'warnings' in my project and I really had only one (simple one):
此错误消息非常令人困惑。我刚刚修复了项目中的其他“警告”,但实际上只有一个(简单的):
warning C4101: 'i': unreferenced local variable
警告 C4101:“i”:未引用的局部变量
After I commented this unused i
, and compiled it, the other error went away.
在我评论这个未使用的i
并编译它之后,另一个错误消失了。
回答by Sergei
This warning is about unsafe use of strcpy. Try IOBname[ii]='\0';
instead.
此警告是关于 strcpy 的不安全使用。试试吧IOBname[ii]='\0';
。