c语言程序中.h和.cpp中遇到的函数嵌套的问题
我现在自己编写了三个小程序,可是编译的时候出现了问题,请教高人帮忙解决一下。。。
一共三个函数
ff2.h中的代码:
# include<stdio.h>
long f2(int q)
{
long c=1;
int i;
for(i=1;i<=q;i++)
c=c*i;
return c;
}
jiecheng.h中的代码:
# include<stdio.h>
# include "ff2.h"
long f1(int p)
{
int k;
long r;
long f2(int);
k=p*p;
r=f2(k);
return r;
}
最后是主程序中的代码:
# include "ff2.h"
# include "jiecheng.h"
void main()
{
int i;
long s=0;
long f1(int);
for(i=1;i<=3;i++)
s=s+f1(i);
printf("d=%ld\n",s);
}
编译后出现的问题是:
--------------------Configuration: hanshu - Win32 Debug--------------------
Compiling...
qiuhe.cpp
e:\研发项目\c-example\hanshu\ff2.h(3) : error C2084: function 'long __cdecl f2(int)' already has a body
执行 cl.exe 时出错.
qiuhe.obj - 1 error(s), 0 warning(s) c 语言
[解决办法]
//ff2.h
#ifndef _FF2_H
#define _FF2_H
#include<stdio.h>
long f2(int q)
{
long c=1;
int i;
for(i=1;i<=q;i++)
c=c*i;
return c;
}
#endif
#ifndef _LOCALE_H
#define _LOCALE_H
....
//头文件中的内容
....
#endif /* _LOCALE_H */