一个简单程序的奇怪错误!

projoy 2009-10-15 03:16:35
用VS2008编译,控制台程序,分为main.c,data.c,data.h三个文件。是编译错误。先把错误贴出来,程序在后面。
1>------ Build started: Project: what, Configuration: Debug Win32 ------
1>Compiling...
1>data.c
1>f:\abc\what\what\data.c(27) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(28) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(29) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(29) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(29) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(30) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(30) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(30) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(31) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(31) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(34) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(35) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(36) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(37) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(37) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(37) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(39) : error C2065: 'true' : undeclared identifier
1>f:\abc\what\what\data.c(40) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(41) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(42) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(42) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(43) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(43) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(43) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(44) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(44) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(44) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(45) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(45) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(45) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(47) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(47) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(47) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(47) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(48) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(48) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(52) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(52) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(53) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(53) : error C2065: 'tempb' : undeclared identifier
1>f:\abc\what\what\data.c(54) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(55) : error C2065: 'UsedValue' : undeclared identifier
1>f:\abc\what\what\data.c(55) : warning C4022: 'memset' : pointer mismatch for actual parameter 1
1>f:\abc\what\what\data.c(55) : error C2065: 'UsedValue' : undeclared identifier
1>f:\abc\what\what\data.c(56) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(56) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(56) : error C2143: syntax error : missing ')' before 'type'
1>f:\abc\what\what\data.c(56) : error C2143: syntax error : missing ';' before 'type'
1>f:\abc\what\what\data.c(56) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(56) : warning C4552: '<' : operator has no effect; expected operator with side-effect
1>f:\abc\what\what\data.c(56) : error C2059: syntax error : ')'
1>f:\abc\what\what\data.c(56) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(56) : error C2143: syntax error : missing ';' before '{'
1>f:\abc\what\what\data.c(60) : error C2065: 'UsedValue' : undeclared identifier
1>f:\abc\what\what\data.c(60) : error C2109: subscript requires array or pointer type
1>f:\abc\what\what\data.c(61) : error C2065: 'UsedValue' : undeclared identifier
1>f:\abc\what\what\data.c(61) : error C2109: subscript requires array or pointer type
1>f:\abc\what\what\data.c(65) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(65) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(65) : error C2065: 'tempa' : undeclared identifier
1>f:\abc\what\what\data.c(66) : error C2065: 'i' : undeclared identifier
1>f:\abc\what\what\data.c(66) : error C2065: 'j' : undeclared identifier
1>f:\abc\what\what\data.c(66) : error C2065: 'tempb' : undeclared identifier
1>Build log was saved at "file://f:\abc\what\what\Debug\BuildLog.htm"
1>what - 61 error(s), 2 warning(s)

data.c
#include "data.h"
#include <time.h>
#include <stdlib.h>
#include <memory.h>
#define SIZE 1000 //最大变量的个数
#define CURSIZE 100
int variable,domain;
double density,relaxation;
double res,nvalue;
struct consistency var[SIZE];
int IsUsed[CURSIZE][CURSIZE];

void IniData(int V,int DOM,double DEN,double REL)
{
variable=V;
density=DEN;
domain=DOM;
relaxation=REL;
res=variable*(variable-1)/2*density;
nvalue=variable*variable*REL;
}

void GenerateCon(int V,int DOM,double DEN,double REL)
{
IniData(V,DOM,DEN,REL);
memset(IsUsed,0,sizeof(IsUsed));
int i;
int j;
for( i=0;i<CURSIZE;++i){
for(j=0;j<CURSIZE;++j){
IsUsed[i][j]=0;
}
};
int tempa;
int tempb;
int i;
for(i=0;i<res;++i){
srand((unsigned)time(NULL));
while(true){
tempa=rand()%variable;
tempb=rand()%variable;
if(tempa>tempb){
tempa=tempa^tempb;
tempb=tempa^tempb;
tempa=tempa^tempb;
}
if(!IsUsed[tempa][tempb]&&(tempa!=tempb)){
IsUsed[tempa][tempb]=1;
break;
}
}
var[i].x1=tempa;
var[i].x2=tempb;
int UsedValue[CURSIZE][CURSIZE];
memset(UsedValue,0,sizeof(UsedValue));
for(int j=0;j<nvalue;++j){
while(1){
int tempa=rand()%domain;
int tempb=rand()%domain;
if(!UsedValue[tempa][tempb]&&tempa!=tempb){
UsedValue[tempa][tempb]=1;
break;
}
}
var[i].value[j].x1_cost=tempa;
var[i].value[j].x2_cost=tempb;
}
}
}
...全文
145 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
projoy 2009-10-15
  • 打赏
  • 举报
回复
答案找到了,引以为戒
今天偶然写了下面的程序(原来我写的程序不一样,下面的只是为了把问题简化)

void foo()

{

int p = 0;

if ( p == 0 ) {

int i = 0;

}



int a;

}



int main()

{

foo();

}



不幸的是偶然将这个文件保存成了test.c

然后编译的时候出现了

error, error C2143: syntax error : missing ';' before 'type'

感觉很奇怪,细细看来所有的语法都似乎都是对的,更奇怪的是把文件改成cpp或者cc能正常编译,把int a;和if调换下也能正常编译。

google了下得到了答案和总结如下:



来自MSDN (http://support.microsoft.com/kb/58559):

这样的错误可能发生在当变量的声明放在可执行代码之后。而这个是在K&R C中规定的,但在ANSI C中废除。

MSDN给出下面的代码会产生这个error

#include <stdio.h>



void main(void)

{

int i;

printf("Hello World\n");

int j;

}



注: The C Programming Language的作者简称K&R,也是C语言之父, 经常用K&R C来和ANSI C做对比。这本书的第二版已经用ANSI.

我用的编译器是VS2008, 看来微软向来无视标准。



总结:

在 ANSI C或者C++中,在可执行代码中随时定义变量是允许的,但是在K&R C中是不允许的,VS2008实现的C竟然是K&R C。

注意这样的错误也体现在VS中要是用for (int i = 0; i++; i<10)同时你的文件名是.c的也会出现这样的错误。

Code complete中讨论过变量名的最迟绑定有利于增加代码的可读性等。所以在VS中写c要注意了


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fancylea/archive/2009/06/10/4256793.aspx
shuilongyin_00 2009-10-15
  • 打赏
  • 举报
回复
不懂,来学习下!
mstlq 2009-10-15
  • 打赏
  • 举报
回复

void GenerateCon(int V,int DOM,double DEN,double REL)
{
IniData(V,DOM,DEN,REL);
memset(IsUsed,0,sizeof(IsUsed));
int i;
int j;
int tempa;
int tempb; //c语言,变量声明必须都放前面……
for( i=0;i<CURSIZE;++i){
for(j=0;j<CURSIZE;++j){
IsUsed[i][j]=0;
}
};

for(i=0;i<res;++i){
srand((unsigned)time(NULL));
while(true){
tempa=rand()%variable;
tempb=rand()%variable;
if(tempa>tempb){
tempa=tempa^tempb;
tempb=tempa^tempb;
tempa=tempa^tempb;
}
if(!IsUsed[tempa][tempb]&&(tempa!=tempb)){
IsUsed[tempa][tempb]=1;
break;
}
}
var[i].x1=tempa;
var[i].x2=tempb;
int UsedValue[CURSIZE][CURSIZE];
memset(UsedValue,0,sizeof(UsedValue));
for(int j=0;j<nvalue;++j){
while(1){
int tempa=rand()%domain;
int tempb=rand()%domain;
if(!UsedValue[tempa][tempb]&&tempa!=tempb){
UsedValue[tempa][tempb]=1;
break;
}
}
var[i].value[j].x1_cost=tempa;
var[i].value[j].x2_cost=tempb;
}
}
}
projoy 2009-10-15
  • 打赏
  • 举报
回复
加上之后也是一样
冻结 2009-10-15
  • 打赏
  • 举报
回复
int i;
int j;
for( i=0;i<CURSIZE;++i){
for(j=0;j<CURSIZE;++j){
IsUsed[i][j]=0;
}
};
int tempa;
int tempb;
int i;
for(i=0;i<res;++i){
srand((unsigned)time(NULL));
while(true){
tempa=rand()%variable;
tempb=rand()%variable;
if(tempa>tempb){
tempa=tempa^tempb;
tempb=tempa^tempb;
tempa=tempa^tempb;
}
if(!IsUsed[tempa][tempb]&&(tempa!=tempb)){
IsUsed[tempa][tempb]=1;
break;
}
}

这里的i重定义了吧。
冻结 2009-10-15
  • 打赏
  • 举报
回复
missing ';'
这种错误一般是缺少包含头文件或前向声明。
wanjingwei 2009-10-15
  • 打赏
  • 举报
回复
把//#include "data.h"这加上不就行了
projoy 2009-10-15
  • 打赏
  • 举报
回复
data.h
#ifndef DATA
#define DATA
#define SIZE 1000
struct cost{
int x1_cost;
int x2_cost;
};

struct consistency{
int x1;
int x2;
struct cost value[SIZE];
};

void IniData(int V,int DOM,double DEN,double REL);
void GenerateCon(int V,int DOM,double DEN,double REL);
#endif

main.c
#include <stdio.h>
//#include "data.h"
int main ()
{
//GenerateCon(7,7,0.4,0.4);
printf("saldkjflsakdjf");
return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧