[C.C++] C++ 在一个cpp文件中使用另一个cpp文件中定义的函数

321 0
Honkers 2026-4-8 01:57:31 | 显示全部楼层 |阅读模式

建立一个console项目

以dev为例,在【文件】【新建】【项目】,新建一个console项目。

头文件

添加新文件,输入说要使用函数的声明,保存为.h文件。

  1. int add(int a,int b);
复制代码


cpp文件

定义在.h中声明的函数,保存为.cpp文件。

  1. #include<iostream>
  2. #include"chen.h"//不要忘记这个
  3. int add(int a,int b)
  4. {
  5. return a + b ;
  6. }
复制代码

main.cpp

在主函数里面调用函数

  1. #include <iostream>
  2. #include "chen.h"
  3. using namespace std;
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5. int main(int argc, char** argv) {
  6. cout << add(3,5) << endl;
  7. return 0;
  8. }
复制代码

运行结果:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行