【问题标题】:error no instance of overloaded function "calMean" matches the argument list错误没有重载函数“calMean”的实例与参数列表匹配
【发布时间】:2013-04-15 05:38:41
【问题描述】:

我有错误说没有重载函数“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 函数调用中编写列表参数列表的方式是错误的,有没有办法解决这个问题?我正在尝试计算结构中成员得分的平均值。

【问题讨论】:

  • 尝试从&amp;count中删除&amp;。
  • 另请注意,听起来您正在尝试像编译 C++ 一样编译 C 代码,因此会出现 C++ 错误消息 - 将 .cpp 后缀更改为 .c 以获得更有意义的 C 特定错误消息.
  • 其实和我之前做的一样,但仍然显示同样的错误,我的源文件也是 .c 而不是 .cpp
  • 你确定这是 C 吗? C 中一般没有重载函数,但 C++ 中有。您是否使用 C++ 编译器(可能是 Windows 上的 MSVC)编译 C 代码?
  • 我也认为我以前从未听说过 C 中的重载函数,只在 java 和 C++ 中使用过

标签: c file structure


【解决方案1】:

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

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

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

...

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

请注意,在 calMean() 首次声明和定义之间,以某种方式重新定义了 RECORD 类型,而不会导致编译错误。

这意味着您有两个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’

【讨论】:

  • 您的源文件是否有.c 后缀(即C 源文件)或.C 或.cpp 后缀(即C++ 源文件)
  • 它是.cpp,如错误消息中所示。我没有尝试将其作为 C 代码;也许我应该? '昨晚很晚......
  • 在x.c 而不是x.cpp 上使用GCC(而不是G++),我得到错误x.c:3:15: error: unknown type name ‘RECORD’,这是我所期望的。在 C++ 中,您只能从 struct RECORD 获得一个普通类型 RECORD,这就是我使用 .cpp 扩展的原因。显然,这不是 MSVC;那可能会做不同的事情。 (编译器命令行:gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -c x.c)
  • 谢谢我想我明白了,它显示错误的原因是因为它把我的函数原型放在 typedef 声明之前
  • 这是其中的一部分,当然;谜题(对我来说)是为什么当原型在 typedef 之前时你没有收到错误。
猜你喜欢
  • 1970-01-01
  • 2014-12-27
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2019-07-16
  • 2013-11-17
  • 1970-01-01
相关资源
最近更新 更多