2013-04-15 56 views
1

我有错误,说没有的重载函数“calMean”实例参数列表匹配匹配错误没有重载函数“calMean”实例参数列表

这是我的代码

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

#define FILE_NAME 20 
#define LIST_SIZE 50 

float calMean(RECORD list[], int count) 

typedef struct 
{ 
    char *name; 
    int score; 
}RECORD; 


int main (void) 
{ 
    // Declarations 
     float mean; 
     FILE *fp; 
     char fileName[FILE_NAME]; 
     RECORD list[LIST_SIZE]; 
     char buffer[100]; 
     int count = 0; 
     int i; 
    // Statements 
     printf("Enter the file name: "); 
     gets(fileName); 

     fp = fopen(fileName, "r"); 

     if(fp == NULL) 
      printf("Error cannot open the file!\n"); 
     while(fgets(buffer, 100, fp) != NULL) 
      { 
      if(count >= LIST_SIZE) 
      { 
       printf("Only the first 50 data will be read!\n"); 
       break; 
      } 
      if(count < LIST_SIZE) 
      { 
       list[count].name = (char*) malloc(strlen(buffer)*sizeof(char)); 
       sscanf(buffer,"%[^,], %d", list[count].name, &list[count].score); 
       printf("name is %s and score is %d\n", list[count].name, list[count].score); 
       count++; 
      } 
      for(i =0; i < (LIST_SIZE - count); i++) 
      { 
      list[count + i].name = 0; 
      list[count + i].score = 0; 
      } 
      } 
     printf("Read in %d data records\n", count); 
     mean = calMean(list, count);   
     fclose(fp); 
     return 0; 
} 


float calMean(RECORD list[], int count) 
{ 
    float tempMean; 
    int sum; 
    int i; 
    for(i = 0; i < count; i++) 
     sum += list[i].score; 
    tempMean = sum/count; 

    return tempMean; 
} 

这个错误发生在main函数调用calMean函数时,我是新来的结构,所以我认为我在calMean函数调用中编写列表参数列表的方式是错误的,有无论如何解决这个问题?我试图计算结构中成员分数的平均值。

+1

尝试删除'&''从&count'。 – Detheroc

+1

还要注意,它听起来像是在尝试编译C代码,就好像它是C++一样,因此C++错误消息 - 将.cpp后缀更改为.c以获得更有意义的特定于C的错误消息。 –

+0

实际上,我以前做过,但它仍然显示相同的错误,也是我的源文件是.c不是.cpp – user1763658

回答

0

麻烦很有趣。你展示的不是你正在编译的代码,这总是坏消息。你有什么是:

float calMean(RECORD list[], int count); // semicolon missing in original 

typedef struct 
{ 
    char *name; 
    int score; 
}RECORD; 

... 

float calMean(RECORD list[], int count) 
{ 
    ... 
} 

注意该类型RECORD以某种方式重新定义 - 而不会引起编译错误 - 的时间之间时calMean()第一次声明,它的定义时。

这意味着您有两个针对calMean()的声明,但它们指的是第一个参数中的不同类型。因此,“超载”的说法。

您必须使用C++编译器。

我想不出一种可以如图所示编写代码的方式,以便RECORD改变这种意思。我想这样的代码:

struct RECORD { int x; }; 

float calMean(RECORD list[], int count); 

typedef struct { int y; } RECORD; 

float calMean(RECORD list[], int count); 

但G ++ 4.7.1说:

x.cpp:5:31: error: conflicting declaration ‘typedef struct RECORD RECORD’ 
x.cpp:1:12: error: ‘struct RECORD’ has a previous declaration as ‘struct RECORD’ 
+0

是的我使用MVS编译器 – user1763658

+0

你的源文件是否有'.c'后缀(即C源文件)或'.C '或'.cpp'后缀(即C++源文件) –

+0

与错误消息中的'.cpp'一样。我没有尝试它作为C代码;也许我应该? '昨晚深夜... –

相关问题