You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

gcc报错:含省略号的参数列表无法匹配空参数名列表声明求解答

解决gcc编译错误:a parameter list with an ellipsis can't match an empty parameter name list declaration

错误原因

编译错误的核心是函数调用与声明不匹配:

  • 在main函数中调用sum_them_all时,编译器找不到该函数的前置声明,会默认推断它是int sum_them_all();(空参数列表的函数)。
  • 但实际定义的sum_them_all带有固定参数n和可变参数...,两者参数列表完全不兼容,因此触发该错误。

另外代码还有两个小问题:

  1. 未包含stdio.h,调用printf会产生编译警告;
  2. 函数注释格式错误,Doxygen风格注释中函数名不需要加@前缀。

修复方案

方案一:在main前添加函数声明

在代码开头、main函数定义前,添加sum_them_all的函数声明:

#include <stdarg.h>
#include <stdio.h>  // 补充printf所需的头文件

// 添加函数声明
int sum_them_all(const unsigned int n, ...);

方案二:将sum_them_all定义移到main之前

直接把sum_them_all的函数定义放在main函数前面,让编译器在调用前就能识别函数的参数列表。

修复后的完整代码示例

#include <stdarg.h>
#include <stdio.h>

/**
 * sum_them_all - calculate the entire sum. 
 * 
 * @n: constant unsigned int param signifying the total number to sum 
 * @...: ellipsis (other parameters)
 * 
 * Return: the sum
 */
int sum_them_all(const unsigned int n, ...)
{
  unsigned int i;
  va_list ag;
  unsigned int sum = 0;

  va_start(ag, n);

  for (i = 0; i < n; i++)
  {
    sum += va_arg(ag, int);  
  }

  va_end(ag);
  return (sum);
}

/**
 * main - check the code
 *
 * Return: Always 0.
 */
int main(void)
{
  int sum;

  sum = sum_them_all(2, 98, 1024);
  printf("%d\n", sum);
  sum = sum_them_all(4, 98, 1024, 402, -1024);
  printf("%d\n", sum);    
  return (0);
}

内容的提问来源于stack exchange,提问作者Eyang Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 20:40:29