在主函数中调用其他文件内的函数
2016-05-19 11:44
260 查看
前提为,另一个文件中的函数,不能是静态函数,即不能有static修饰。
调用方法,在调用前进行声明,然后直接调用即可。
声明方法:
1 直接在调用前,写函数声明:
如调用函数为int func(int a),那么在调用前只需要
int func(int a);
这样声明后,即可使用func。
2 将声明写在头文件中。
如在名为func.h的头文件中加入
int func(int a);
然后在调用的源文件中,引用
#include "func.h"
即可调用func。
就是再写个头文件,对调用文件里的函数进行申明。再将头文件写在主函数所在的文件里。如下面所示:
//文件a.c
#include "a.h"
int display(int a,int b){
cout<<a+b<<endl;
return 0;
}
//文件a.h
#include <iostream>
int display(int ,int );
//文件main.c
#include "a.h"
int main(){
int a=2,b=3;
display(a,b);
return 0;
}
简单写几行,使用非本源文件的函数就需要文件包含方式来包含其他文件,注意好头文件别重复
调用方法,在调用前进行声明,然后直接调用即可。
声明方法:
1 直接在调用前,写函数声明:
如调用函数为int func(int a),那么在调用前只需要
int func(int a);
这样声明后,即可使用func。
2 将声明写在头文件中。
如在名为func.h的头文件中加入
int func(int a);
然后在调用的源文件中,引用
#include "func.h"
即可调用func。
就是再写个头文件,对调用文件里的函数进行申明。再将头文件写在主函数所在的文件里。如下面所示:
//文件a.c
#include "a.h"
int display(int a,int b){
cout<<a+b<<endl;
return 0;
}
//文件a.h
#include <iostream>
int display(int ,int );
//文件main.c
#include "a.h"
int main(){
int a=2,b=3;
display(a,b);
return 0;
}
简单写几行,使用非本源文件的函数就需要文件包含方式来包含其他文件,注意好头文件别重复
相关文章推荐
- Execution failed for task ':xxx:packageAllDebugClassesForMul tiDex'. > java.util
- Jackson tips: using @JsonAnyGetter/@JsonAnySetter to create "dyna beans"
- [.NET Core].NET Core R2安装教程及Hello示例
- ipmitool 查看硬件信息
- 运行JBoss 5.1.0 GA时出现Error installing to Instantiated:name=AttachmentStore state=Described错误的解决办法
- 移动平台—企业转型需要的核心驱动力
- Hibernate——HQL数据查询
- 在ASP.NET 2.0中操作数据之七十二:调试存储过程
- Spark定制班第7课:Spark Streaming源码解读之JobScheduler内幕实现和深度思考
- Hexo个人博客搭建问题之 './build/Release/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND'
- Xml表中Vector3类型数据取出
- openstack之neutron linuxbridge + vlan组网
- struts2 开发
- Android动画进阶(Interpolator)
- ASP.NET 用户控件实现后台确认提醒框
- pc直接粘贴到div图片
- ucenter实现原理
- Canvas填充样式fillStyle
- String的hashcode(java)
- 传感器--方向传感器制作指南针