【发布时间】:2019-04-12 13:09:42
【问题描述】:
我有问题。 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 argCount, ...); // prototype代替RastgeleKarakter SKarakterOlustur(int...); // prototype -
在 .h 和 .c 文件中将 "," 放在 "..." 之前。
RastgeleKarakter SKarakterOlustur(int a, ...) -
是的。他们工作了。谢谢。
-
请注意,使用可变参数函数是不好的做法。如果您尝试在 C 中执行 OO,尤其如此。