所以我有三个文件,main.c, ah, bh。
在所有三个文件中,我都声明了以下函数
int test(int a, int b)
{
if(a>b)
return a;
else return b;
}
无论如何我可以在主程序中使用该函数,但来自不同的位置(从 ah,然后从 bh,然后从 main.c),因为现在我有这个错误:错误:重新定义测试?
为了更好地理解,我将在此处发布声明:
在两个库中编写 test() ah 和 bh 在一个程序中包含这两个库。调查如何使用 ah 或 bh 中的 test() 如果我也在主程序中定义 test() 并且我想使用主程序、库 ah 或 bh 中的 test() 怎么办?
main.c 中的代码是这样的:
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
#include "b.h"
int test(int a, int b) //Function for showing the max
{
if(a > b)
return a;
else return b;
}
int main()
{
int a, b;
scanf("%d %d", &a, &b);
test(a, b);
return 0;
}
ah 中的代码与 bh 中的代码相同:
#ifndef CODE_a_H
#define CODE_a_H
int test(int a, int b) //Function for showing the max
{
if(a > b)
return a;
else return b;
}
#endif
对被告有疑问。
也许你应该在这个作业中学到一些东西。而“某事”可能是“不要这样做”。
您显然已经学到的是,由于重复的符号,这将不起作用。
您可以在预处理器的帮助下生成工作程序。
将定义扩展test()
为
#ifndef CODE_a_H
#define CODE_a_H
#ifndef TEST_DEFINED
#define TEST_DEFINED
int test(int a, int b) //Function for showing the max
{
if(a > b)
return a;
else return b;
}
#endif
#endif
根据#include
主程序中的顺序,您包含的第一个文件的定义将在程序中结束。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句