c - C 新手,错误 C2371 : 'error" : redefinition; diffrent basic types

标签 c redefinition

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>



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;

这些是有问题的函数和主要:

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 给出相同的错误。

请尝试帮助和指导我,我很困惑。 tnx。

最佳答案

您的问题之一是使用 CommandSwitch 中的 error 函数。

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 的隐式函数声明功能,它允许您像隐式声明一样使用函数,因为您没有使用函数原型(prototype)。

对于编译器来说,它的行为就好像有一个函数声明为

int error(...);

这与您的功能有冲突:

static void error(char *);

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

解决此问题的最简单方法至少是将 error 函数移至 void CommandsSwitch 之前。

您将需要阅读有关函数声明和原型(prototype)的内容:

关于c - C 新手,错误 C2371 : 'error" : redefinition; diffrent basic types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4187038/

相关文章:

python - 如何在 Python 中包装返回位掩码的函数

c - 如何在 Ubuntu 上给出 EOF,Ctrl-D 似乎不起作用?

C++类重定义错误求助

c - tcc 中的打包结构

c - 为什么我不能同时 printf 和 scanf 值

c - 为什么将变量移位超过其位宽度会清零?

c++ - 重载函数、重新定义、C2371 和 C2556 C++

c++ - 即使有守卫在场,编译器也会提示重新定义

c++ - 单个类出现类重定义错误

xcode - 尝试构建用于发布的 XCode 项目时出现 Typedef 重定义错误