编译器出现此错误:
1>....\server\sv_init.c(528): error C2143: syntax error : missing ';' before 'type' 1>....\server\sv_init.c(529): error C2065: 'v' : undeclared identifier
...(all instruction lines containing v)
这是部分代码:
while(shl>=7) {
shl-=7;
int v = (sh>>shl)&127;// <-- Error is here
if (v==0 || v=='"' || v=='%' || v=='@') {
tmp[ol++] = '@';
// Com_Printf("OUT:%02X\n",tmp[ol-1]);
if (ol==sizeof(tmp)-1) {
tmp[ol]=0;
if (csnr==PURE_COMPRESS_NUMCS) {
Com_Printf(err_chunk);
return 1;
}
SV_SetConfigstring( MAX_CONFIGSTRINGS-PURE_COMPRESS_NUMCS+csnr, tmp);
csnr++;
ol=0;
}
tmp[ol++] = v+1;
} else {
tmp[ol++] = v;
}
我尝试删除错误行之前的行,代码构建良好,任何帮助或建议都将受到欢迎。
最佳答案
您的代码正在 C 模式下编译(因为该文件具有 .c 扩展名)。在 C 模式中,不能在语句之后声明变量。您有三个选择:
- 更改代码以在封闭范围的开头声明变量,如下所示:
while(shl>=7) { int v; // declaration shl-=7; v = (sh>>shl)&127;
如果有很多这样的代码,这可能会需要大量工作。
- 通过将文件扩展名更改为 .cpp 或指定/TP 选项,以 C++ 模式进行编译。这可能有效,因为 C++ 主要是 C 的超集,但有一些 C 构造是 C++ 不允许的。
- 使用在 C 语言中支持此功能的其他编译器,例如 GCC。
关于c - MsVc++奇怪的错误: not declared variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339214/