我有个问题。 C语言中面向对象编程的概念得到了作业。我需要使用可变函数。但是我弄错了。如果你能帮助我,我会很感激的。我是编码的新手。
rastgelekarakter.h:
#ifndef RASTGELEKARAKTER_H
#define RASTGELEKARAKTER_H
struct RASTGELEKARAKTER{
// code
};
RastgeleKarakter SKarakterOlustur(int...); // prototype
void Print(const RastgeleKarakter);
#endif
rastgelekarakter.c:
#include "RastgeleKarakter.h"
#include "stdarg.h
RastgeleKarakter SKarakterOlustur(int... characters){
//code
}
错误:
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...);
我不知道有多少参数。我想用变量函数来解决这个问题。
参数列表不应具有类型或名称
RastgeleKarakter SKarakterOlustur(int count, ...)
{
va_list args;
va_start(args, count);
int i = va_arg(args, int);
}
使用stdarg.h
头文件中定义的宏来访问参数列表。 further reading
如果你原来的减速,你的意思是参数列表的所有成员都是整数,因为无论如何你将提供计数,考虑将其改为int count, int * list
C中的变量参数是无类型和未命名的。可变函数的正确原型是:
returnType functionName(type1 ordinaryArg1, type2 ordinaryArg2, ...)
在...
之前你需要至少一个普通的参数。您只能通过stdarg.h
中的函数访问可变参数。
该错误表示编译器在省略号之前需要以下其中一项:-semicolon -comma -closing括号
因此,原型未正确声明。声明至少需要一个命名变量,最后一个参数必须是省略号。
例如,如果您打算将整数传递给方法,则声明可能如下所示:
int sum (int count, ...);