2013-04-15 58 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代碼;也許我應該? '昨晚深夜... –

相關問題