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

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

建立一个console项目

以dev为例,在【文件】【新建】【项目】,新建一个console项目。
在这里插入图片描述

头文件

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

	int add(int a,int b); 

在这里插入图片描述
在这里插入图片描述

cpp文件

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

	#include<iostream>
	#include"chen.h"//不要忘记这个
	int add(int a,int b)
	{
    
    
		return a + b ;
	} 

main.cpp

在主函数里面调用函数

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

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_43456002/article/details/106186156