我不知道从Visual Studio 2010收到这些错误的原因。
这是我的程序从第343行到第408行的代码:
int create_den_from_img(char *img_file_name_part, int xlen, int ylen, int zlen )
{
IplImage* imgs = 0;
char str[80];
unsigned char *data,*imgdata;
/* allocating memory */
data = (unsigned char *) malloc(xlen * ylen * zlen * sizeof(unsigned char) );
if(data==NULL)
{
printf("error in allocating memory \n");
exit(1);
}
/* Getting the filename & iterating through tiff images */
for(int k = 0; k < zlen; k++)
{
int count=2;
int tmp=k+1;
while(tmp/10)
{
count=count-1;
tmp=tmp/10;
}
switch(count)
{
case 2:sprintf(str,"%s00%d.tif",img_file_name_part,k+1);
break;
case 1:sprintf(str,"%s0%d.tif",img_file_name_part,k+1);
break;
default:sprintf(str,"%s%d.tif",img_file_name_part,k+1);
break;
}
printf("%s\n",str);
/* Loading Image using OpenCV */
imgs=cvLoadImage(str,-1);
if(imgs==NULL)
{
printf("error in opening image \n");
exit(1);
}
imgdata=(uchar *)imgs->imageData;
for(int j =0; j < ylen; j++)
{
for(int i =0; i < xlen; i++)
{
data[ k*xlen*ylen + j*xlen + i ] = imgdata[ j*xlen+i ];
}
}
cvReleaseImage(&imgs );
}
/* populating `data` variable is done. So, calling `write_den` */
if(write_den("test.den",data,xlen,ylen,zlen)==0)
{
printf("Error in creating den file\n");
exit(1);
}
printf("Den file created\n");
}
这些是错误列表:
Error 3 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 358 1 MTP_TEST Error 4 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 358 1 MTP_TEST Error 5 error C2143: syntax error : missing ')' before 'type' c:\examples\denfile.c 358 1 MTP_TEST Error 6 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 358 1 MTP_TEST Error 7 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 358 1 MTP_TEST Error 9 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 358 1 MTP_TEST Error 10 error C2059: syntax error : ')' c:\examples\denfile.c 358 1 MTP_TEST Error 11 error C2143: syntax error : missing ';' before '{' c:\examples\denfile.c 359 1 MTP_TEST Error 12 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 361 1 MTP_TEST Error 13 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 370 1 MTP_TEST Error 14 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 372 1 MTP_TEST Error 15 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 374 1 MTP_TEST Error 16 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 388 1 MTP_TEST Error 17 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 388 1 MTP_TEST Error 18 error C2143: syntax error : missing ')' before 'type' c:\examples\denfile.c 388 1 MTP_TEST Error 19 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 388 1 MTP_TEST Error 20 error C2065: 'j' : undeclared identifier c:\examples\denfile.c 388 1 MTP_TEST Error 22 error C2065: 'j' : undeclared identifier c:\examples\denfile.c 388 1 MTP_TEST Error 23 error C2059: syntax error : ')' c:\examples\denfile.c 388 1 MTP_TEST Error 24 error C2143: syntax error : missing ';' before '{' c:\examples\denfile.c 389 1 MTP_TEST Error 25 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 390 1 MTP_TEST Error 26 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 390 1 MTP_TEST Error 27 error C2143: syntax error : missing ')' before 'type' c:\examples\denfile.c 390 1 MTP_TEST Error 28 error C2143: syntax error : missing ';' before 'type' c:\examples\denfile.c 390 1 MTP_TEST Error 29 error C2065: 'i' : undeclared identifier c:\examples\denfile.c 390 1 MTP_TEST Error 31 error C2065: 'i' : undeclared identifier c:\examples\denfile.c 390 1 MTP_TEST Error 32 error C2059: syntax error : ')' c:\examples\denfile.c 390 1 MTP_TEST Error 33 error C2143: syntax error : missing ';' before '{' c:\examples\denfile.c 391 1 MTP_TEST Error 34 error C2065: 'k' : undeclared identifier c:\examples\denfile.c 392 1 MTP_TEST Error 35 error C2065: 'j' : undeclared identifier c:\examples\denfile.c 392 1 MTP_TEST Error 36 error C2065: 'i' : undeclared identifier c:\examples\denfile.c 392 1 MTP_TEST Error 37 error C2065: 'j' : undeclared identifier c:\examples\denfile.c 392 1 MTP_TEST Error 38 error C2065: 'i' : undeclared identifier c:\examples\denfile.c 392 1 MTP_TEST
我整天都在犯这些错误。有时代码编译,而在其他时候它不编译。它真的很烦人。
答案 0 :(得分:13)
您正在编译.c
文件,对于Microsoft Visual Studio,这意味着您需要编写C89(又名C90)代码,而不是C99代码或C ++。
这意味着您必须在每个块的开头声明变量。这意味着您无法做到:
for (int k = 0; ...
您必须在块的开头声明k
并执行:
for (k = 0; ...
答案 1 :(得分:2)
在第343行之前的类/结构声明后,您是否缺少分号?
您可以做的一件事是尝试使用另一个编译器来查看是否收到了一条对您说得更多的错误消息。例如,有Comeau online。
否则,你的代码或部分内容中是否有一些流氓#defines
?既然这不会一直发生,有人可能会骗你吗?尝试检查预处理的输出。