日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++兩個cpp文件間如何進行各自函數的調用方式_C 語言

作者:「已注銷」 ? 更新時間: 2023-06-05 編程語言

兩個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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新