2012-01-27 44 views
0

在C程序中,我遇到了一个令我非常沮丧的错误。通过引用,使用不同基类型的错误

错误是:

main.c(65): error C2371: 'extractVals' : redefinition; different basic types 

directClass是假设接受为一个字符的参考,特别是第20行(在开关壳体的“g”),使用变量s。我不知道他们是如何不是基本类型,但我不是很好与C,所以我没有很好地确定所有问题。任何帮助都会很棒。

#include <gl\glew.h> 
#include <gl\freeglut.h> 
#include <gl\GLU.h> 
#include <stdio.h> 

void directFile(char input[100]){ 
    char switchVal [10] , *s = switchVal; 
    float val1, val2, val3, val4; 

    s = strtok(input, " \n\0"); 

    printf("Told str is %s\n", s); 
    switch(*s){ 

     case '#': 
      printf("%s is a comment. Has no bearing on application\n", s); 
      break; 
     case 'g': 
      printf("%s is the command to translate an object!\n", s); 
      extractVals(s); 
      break; 
     case 's': 
      printf("%s is the command to scale, now which one is it?\n",s); 
      break; 
     case 'r': 
      printf("%s will rotate the image!\n",s); 
      break; 
     case 'c': 
      if(strcmp(s , "cone") == 0){ 
       printf("It appears you have your self a %s\n", s); 
      } else if (strcmp(s , "cube") == 0){ 
       printf("%s is cool too\n" , s); 
      } else if (*s == 'c'){ 
       printf("Welp command was \"%s\", lets change some colors huh?\n",s); 
      } 
      break; 
     case 't': 
      break; 
     case 'o': 
      break; 
     case 'f': 
      break; 
     case 'm': 
      break; 
    } 
} 

void extractVals(char *input){ 
    while(input != NULL){ 
     printf("%s\n", input); 
     input = strtok(NULL, " ,"); 
    } 

} 

void makeLower(char *input) 
{ 
    while (*input != '\0') 
    { 
     *input = tolower(*input); 
     input++; 
    } 
} 


int main(int argc, char *argv[]) { 
    FILE *file = fopen(argv[1], "r"); 
    char linebyline [50], *lineStr = linebyline; 
    char test; 

    glutInit(&argc, argv); 

    while(!feof(file) && file != NULL){ 
     fgets(lineStr , 50, file); 
     makeLower(lineStr); 
     printf("%s",lineStr); 

     directFile(lineStr); 

    } 
    fclose(file); 


    glutMainLoop(); 
} 
+0

为什么它给重新定义错误奇怪的顶部添加一个原型的功能,它应该给一个错误是这样的:“功能extractVals未找到”。在实际声明之前你正在调用一个函数。 – 2012-01-27 20:09:21

+0

C不支持通过引用,但C++。 C使用指针。 http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference – 2012-01-27 20:38:31

回答

3

你的错误是因为你没有打电话extractVals()前提供一台样机。当编译器运行到这样的情况下,假定函数声明如下:

int extractVals(); 

再后来,当它找到的定义,它与这一假设冲突。这个错误可以通过添加适当的原型是固定的,无论是directFile()之前或在您包括头:

void extractVals(char *input); 
+1

如果增加警告级别'-Wall',编译器会在第一次遇到extractVals时发出警告,并且您将避免混淆。 – Ben 2012-01-27 20:17:14

+0

哦。多么简单。我一直在做太多的Java! – meriley 2012-01-27 20:50:16

0

我相信编译器是困惑,因为它看到的功能extractVals第一次是在功能directFile。编译器最有可能假定函数是(int)blah(char *)。然后,当它下降到你的函数定义不同时,编译器会抛出一个错误。尽管我会认为这会引发对函数错误的重新定义,而不是基本类型。

无论哪种方式,都试图在文件

void extractVals(char *input); // I let the compiler know that a function with this signature is coming later 

void directFile(char input[100]){ 
char switchVal [10] , *s = switchVal; 
float val1, val2, val3, val4; 
...