新的C,错误C2371:“错误" :重新定义;不同势基本类型 [英] new to C , error C2371: 'error" : redefinition; diffrent basic types

查看:650
本文介绍了新的C,错误C2371:“错误" :重新定义;不同势基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提交此assigment在几个小时内,我非常紧张,
这有点加油站管理程序,HANDELING输入文件和打印结果...
它只有1 .c文件,这就是我的第一个code线,它定义了结构

i've to submit this assigment in few hours and i'm very nervous, it's sort of Gas Station managing programs, handeling input files, and printing results... it's only 1 .c file and that's my first code lines, which defines the structs

#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;

和那些有问题的功能和主要的:

and those are the problematic functions and the main :

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);

}

现在的错误指向静态无效的错误(字符* MSG)的功能,但在此之前,它指向无效CommandsSwitch(字符*订单) CommandsSwitch 给予同样的错误。

now the error points to the static void error(char *msg) function but before that it pointed to void CommandsSwitch(char *orders), the CommandsSwitch give the same error.

PLZ尽力帮助和引导我,我很困惑。
TNX。

plz try to help and guide me, i'm confused. tnx.

推荐答案

你的一个问题是你的错误的使用函数 CommandSwitch

One of your problems is your use of the error function in CommandSwitch.

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... */

您使用错误函数的错误函数的实际申报前进一步下跌:

You use this error function before your actual declaration of your error function further down:

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

您碰到了C的隐函数声明功能,它可以让你用一个函数,就好像它是隐式声明的,因为你不使用函数原型。

You ran into the implicit function declaration feature of C, which allows you to use a function as if it were implicitly declared, as you are not using function prototypes.

要编译器,它的行为就好像有声明为

To the compiler, it acts as though there's a function declared as

int error(...);

这是与你​​的函数有冲突:

which is a conflict with your function:

static void error(char *);

所以基本上,code充当虽然已经有一个叫做函数错误宣布,并用默认的​​返回类型 INT 。那么编译器将运行到无效错误()函数的声明,并抱怨说,有功能的重新定义错误

So basically, the code acts as though there was already a function called error declared, and with a default return type of int. Then the compiler would run into your void error() function declaration, and complain that there was a redefinition of the function error.

为您解决这个问题的最简单的方法,至少是无效CommandsSwitch 错误功能C $ C>。

您想了解函数声明和原型:

You will want to read about function declarations and prototypes:

  • http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc07fundec.htm
  • http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Functions
  • Function Prototypes

这篇关于新的C,错误C2371:“错误&QUOT; :重新定义;不同势基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆