简体   繁体   English

C错误:在'...'标记之前预期';',','或')'

[英]C error: expected ';', ',' or ')' before '…' token

I have a problem. 我有个问题。 The concept of Object Oriented Programming in C got homework. C语言中面向对象编程的概念得到了作业。 I need to use variadic functions. 我需要使用可变函数。 But I get a mistake. 但是我弄错了。 I'd appreciate it if you could help me. 如果你能帮助我,我会很感激的。 I'm new to encoding. 我是编码的新手。

RastgeleKarakter.h : RastgeleKarakter.h:

#ifndef RASTGELEKARAKTER_H
#define RASTGELEKARAKTER_H
struct RASTGELEKARAKTER{
// code
};

RastgeleKarakter SKarakterOlustur(int...); // prototype
void Print(const RastgeleKarakter);
#endif

RastgeleKarakter.c : RastgeleKarakter.c:

#include "RastgeleKarakter.h"
#include "stdarg.h
RastgeleKarakter SKarakterOlustur(int... characters){
//code
}

Error : 错误:

 make
gcc -I ./include/ -o ./lib/test.o -c ./src/Test.c
In file included from ./src/Test.c:3:0:
./include/RastgeleKarakter.h:17:38: error: expected ';', ',' or ')' before '...' token
RastgeleKarakter SKarakterOlustur(int...);

I don't know how many parameters there are. 我不知道有多少参数。 I want to solve this with the variable function. 我想用变量函数来解决这个问题。

Variadic arguments in C are untyped and unnamed. C中的变量参数是无类型和未命名的。 The correct prototype for a variadic function is: 可变函数的正确原型是:

returnType functionName(type1 ordinaryArg1, type2 ordinaryArg2, ...)

You need at least one ordinary arguments before the ... . ...之前你需要至少一个普通的参数。 You can only access the variadic arguments through the functions from stdarg.h . 您只能通过stdarg.h的函数访问可变参数。

The parameter list should not have a type nor a name 参数列表不应具有类型或名称

RastgeleKarakter SKarakterOlustur(int count, ...)

{
  va_list args;
  va_start(args, count);
  int i = va_arg(args, int);
}

Use the macros defined in stdarg.h header file to access the parameter list. 使用stdarg.h头文件中定义的宏来访问参数列表。 further reading 进一步阅读

If by your original deceleration, you meant that all members of the parameter list are integers, and since you will be supplying the count anyway, consider changing it to int count, int * list 如果你原来的减速,你的意思是参数列表的所有成员都是整数,因为无论如何你都要提供计数,考虑将它改为int count, int * list

The error says that the compiler expects one of the following before the ellipsis: -semicolon -comma -closing parentheses 该错误表示编译器在省略号之前需要以下其中一项:-semicolon -comma -closing括号

So, the prototype is not declared correctly. 因此,原型未正确声明。 The declaration needs at least one named variable and the last parameter must be the ellipsis. 声明至少需要一个命名变量,最后一个参数必须是省略号。

For example, if you intend to pass integers to the method, the declaration could be as follows: 例如,如果您打算将整数传递给方法,则声明可能如下所示:

int  sum (int count, ...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM