这是一个头文件
#include <stdio.h>
int m = 18;
int x = 4;
int singles (n) {
if (n == 1)
return 0;
return doubles(n-1);
}
int doubles (n) {
if (n == 1)
return 0;
return triples(n-1);
}
int triples (n) {
if (n == 1)
return m;
return (singles(n-1) + doubles (n-1) + triples (n-1))*(m-1);
}
Run Code Online (Sandbox Code Playgroud)
这是主文件
#include <stdio.h>
#include "test.h"
int main () {
printf("%d",singles (x));
}
Run Code Online (Sandbox Code Playgroud)
所以至少对我来说这是非常复杂的.这个想法是在主函数中我将调用单个(x),其中x = 4,因此它更像单个(4),它将调用双精度(3),它将调用三倍(2),它将调用所有将返回0的单个(1),返回0的双倍(1)和将返回m的三倍(1).
所以我得到的错误是
./test.h:13:12: warning: implicit declaration of function 'doubles' is invalid
in C99 [-Wimplicit-function-declaration]
return doubles(n-1);
^
./test.h:20:12: warning: implicit declaration of function 'triples' is invalid
in C99 [-Wimplicit-function-declaration]
return triples(n-1);
^
2 warnings generated.
Run Code Online (Sandbox Code Playgroud)
我尝试使用第一个脚本创建头文件.h然后制作第二个.c脚本,我尝试编译,这将无法工作.我尝试导入标头,以尽量避免此错误但它似乎没有工作.非常感谢
dbu*_*ush 10
在singles你doubles的定义之前,你正在使用它.同样地doubles,你在triples定义它之前使用它.这就是你得到隐式声明错误的原因.
此外,您没有n为这些函数中的任何一个定义参数的类型.
您需要指定函数原型,它声明函数而不定义它:
int singles(int n);
int doubles(int n);
int triples(int n);
Run Code Online (Sandbox Code Playgroud)
此外,您不应在头文件中定义函数.如果将此标头包含在多个.c文件中,然后将它们链接在一起,则会出现错误,因为您将对这些函数进行多次定义.
获取所有函数定义并将它们放在test.c中.然后在test.h中,只放上面的原型.然后你可以编译所有内容如下:
gcc -c test.c
gcc -c main.c
gcc -o main main.o test.o
Run Code Online (Sandbox Code Playgroud)
或者在一行中:
gcc -o main test.c main.c
Run Code Online (Sandbox Code Playgroud)