C++ uses a function defined in a cpp file in another cpp file

C++ uses a function defined in a cpp file in another cpp file

Create a console project

Take dev as an example, create a new console project in [File] [New] [Project].
Insert picture description here

head File

Add a new file, enter a statement that says you want to use the function, and save it as a .h file.

	int add(int a,int b); 

Insert picture description here
Insert picture description here

cpp file

Define the functions declared in .h and save them as .cpp files.

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

main.cpp

Call the function in the main function

	#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;
	}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_43456002/article/details/106186156