網站首頁 編程語言 正文
兩個cpp文件間如何進行各自函數的調用
c++文件調用c++文件
前提
- 文件1:main.cpp
- 文件2:called.cpp
問題描述
想要在 main.cpp 代碼中調用 called.cpp 文件中的函數,要怎么做?
解決方法
在網上搜尋了許多方法后,筆者才發現了一個行之有效且簡單的解決方法。
只需要在main.cpp文件中添加兩行代碼即可。如下:
//main.cpp
#include<iostream>
#include "call.cpp" //只需在頭文件中添加該行代碼,即可調用
using namespace std;
int main()
{
? ? called(); //進行調用
? ? return 0;
}
//call.cpp
#include<iostream>
using namespace std;
void called(){
? ? ?cout<<"Hello world!"<<endl;
}
C++分文件調用自己寫的函數
創建.h的頭文件
在頭文件內編寫:
#include <iostream>
using namespace std;
int F_max(int a, int b);
創建.cpp的源文件
在源文件被編寫:先要與頭文件關聯起來加入 ”xxx.h“
#include "FindMax.h"
int F_max(int a, int b) {
return a > b ? a : b;
}
在頭文件寫函數的聲明
在源文件中寫函數的定義(內容)
在任意文件調用
調用的時候只需要加上頭文件就可以了
總結
原文鏈接:https://blog.csdn.net/weixin_51110161/article/details/118722967
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-10-14 composer -vvv 命令
- 2023-05-29 React優雅的封裝SvgIcon組件示例_React
- 2022-02-07 virtualenvwrapper 解決安裝報錯,virtualenvwrapper 永久生效
- 2022-12-03 Golang基于sync.Once實現單例的操作代碼_Golang
- 2022-05-22 go中string、int、float相互轉換的實現示例_Golang
- 2022-04-24 一起來學習C語言的字符串轉換函數_C 語言
- 2022-08-05 Entity?Framework主從表的增刪改_C#教程
- 2022-04-17 python 提取出字符串括號中的內容
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支