新到C,错误C2371:’错误’:重新定义;不同的基本类型

我要在几个小时内提交这个分配,我非常紧张,这是加油站管理程序,处理输入文件和打印结果…它只有1 .c文件,这是我的第一个代码行,它定义了结构

#include  #include  #include  struct Gas_Station *pgasStationHead = NULL; typedef struct Gas_Station { char *name; double octan95SS; double octan95FS; double octan98SS; double octan98FS; double gasSoldTotal; double gasSoldSS; double gasSoldFS; struct Gas_Station* pgasStationNext; struct Client_List* pclientHead; } Station; typedef struct Client_List { char carID[10]; char gasType[3]; double gasAmount; char serviceType[12]; struct Client_List* pclientNext; } Client; 

这些是有问题的function和主要:

 void CommandsSwitch(char *orders) { FILE *input , *output; input = fopen(orders, "rt"); output = fopen("result.txt" , "wt"); if (input == NULL) { error("can't open file, might not exists"); } else if (output == NULL) { error("can't open file"); } else { do { int i; char *ptemp, *pfuncNum, *pcarID , *pstationName; ptemp = fgets(ptemp , 80 , input); if (ptemp[0] != '#') { pfuncNum = strtok(ptemp , ","); i = (int)pfuncNum[0]; switch (i) { case 1: HowMuchGasPerStation(output); break; case 2 : pstationName = strtok(pstationName , ","); AverageGasInSpecieficStation(output , pstationName); break; case 3 : HowMuchGasInAllStations(output); break; case 4 : HowMuchGasFSInAllStations(output); break; case 5 : pcarID = strtok(ptemp , ","); HowMuchGasSoldByCarID(output , pcarID); break; case 6 : pcarID = strtok(ptemp , ","); pstationName = strtok(pstationName , ","); HowMuchGasSoldByStationPerCarID(output , pcarID , pstationName); break; case 7 : pcarID = strtok(ptemp , ","); StationsWithClientByCarID(output , pcarID); break; case 8 : pcarID = strtok(ptemp , ","); pstationName = strtok(pstationName , ","); HowMuchClientSpentByStation(output , pcarID , pstationName); break; case 9 : pcarID = strtok(ptemp , ","); HowMuchClientSpentInTotalByCarID(output , pcarID); break; case 10 : pstationName = strtok(pstationName , ","); ClientDetailsBySpecieficStation(output , pstationName); break; } } }while(!feof(input)); } fclose(input); fclose(output); } static void error(char *msg) { fprintf(stderr , "Error: %s\n", msg); exit(1); } int main (int argc, char* argv[]) { int i; FILE *f; char *orders = argv[1]; for (i = 2; i < argc; i++) { f = fopen(argv[i] , "rt"); if (f == NULL) { error("can't open file, might not exists"); } else { AddStation(f); } fclose(f); } CommandsSwitch(orders); } 

现在错误指向static void error(char *msg)函数,但在此之前它指向void CommandsSwitch(char *orders)CommandsSwitch给出相同的错误。

PLZ试着帮助和指导我,我很困惑。 TNX。

您的一个问题是您在CommandSwitch使用errorfunction。

 void CommandsSwitch(char *orders) { FILE *input , *output; input = fopen(orders, "rt"); output = fopen("result.txt" , "wt"); if (input == NULL) { error("can't open file, might not exists"); } else if (output == NULL) { error("can't open file"); } /* ...more... */ 

在实际声明error函数之前,请使用此error函数:

 static void error(char *msg) { fprintf(stderr , "Error: %s\n", msg); exit(1); } 

您遇到了C的隐式函数声明function,它允许您使用函数,就好像它是隐式声明的那样,因为您没有使用函数原型。

对于编译器来说,它就像声明了一个函数一样

 int error(...); 

这与你的function有冲突:

 static void error(char *); 

所以基本上,代码就好像已经声明了一个名为error的函数,并且默认返回类型为int 。 然后编译器会遇到你的void error()函数声明,并抱怨函数error有重新定义。

至少,修复此问题的最简单方法是在void CommandsSwitch之前移动error函数。

您将需要阅读有关函数声明和原型的信息:

这对你的编译时错误没有帮助,但当你到达可以尝试运行这段代码的时候,你应该知道你没有为ptemp分配任何内存 – 它只是一个悬空指针 。 更改定义:

 char *ptemp, *pfuncNum, *pcarID, *pstationName; 

例如

 char ptemp[LINE_MAX]; char *pfuncNum, *pcarID, *pstationName;