【问题标题】:new to C , error C2371: 'error" : redefinition; diffrent basic typesC 新手,错误 C2371: 'error" : 重新定义;不同的基本类型
【发布时间】:2010-11-15 17:20:06
【问题描述】:

我要在几个小时内提交这个作业,我很紧张, 它有点像加油站管理程序、处理输入文件和打印结果...... 它只有 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。

【问题讨论】:

  • 您可能在某处遗漏了一个符号(例如分号)。这类事情可能会在意想不到的地方导致奇怪的错误。
  • 好的...但是现在它是运行时错误,并且似乎 fopen 的调用在执行它,因为调用堆栈引用了那个..从中获取文件指针的正确方法是什么argv[] ???

标签: c redefinition


【解决方案1】:

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

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

int error(...);

这与你的功能有冲突:

static void error(char *);

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

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

你会想了解函数声明和原型:

【讨论】:

  • 好的...但是现在它是运行时错误,并且似乎 fopen 的调用在执行它,因为调用堆栈引用了那个..从中获取文件指针的正确方法是什么argv[] ???
  • @Kobby:我不确定您是否可以不接受我的回答,但请这样做并用您的新错误更新您的问题。我试图解决你的直接问题,但我确信会有更多错误发生。
【解决方案2】:

这对您的编译时错误没有帮助,但是当您真正可以尝试运行此代码时,您应该知道您没有为 ptemp 分配任何内存 - 它只是一个 @ 987654321@。更改定义:

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

例如

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2010-09-07
    相关资源
    最近更新 更多