如何从另一个.cpp文件中的一个.cpp文件调用函数? [英] How to call functions from one .cpp file in another .cpp file?

查看:56
本文介绍了如何从另一个.cpp文件中的一个.cpp文件调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试查找此文件,并使用头文件等获得了混合结果.

I tried looking this up and got mixed results using header files and such.

基本上,我有多个cpp文件,这些文件具有用于二叉树,BST,链接列表等的所有功能.

Basically I have multiple cpp files with all my functions I made for use with binary trees, BST, linked lists etc.

我想做的就是不必复制和粘贴函数,而只是想做一个

What I want to do is instead of having to copy and paste functions as I need them I want to just be able to do a

#include <myBSTFunctions.h> 

并能够调用和使用我自己的功能.

And be able to call and use my own functions.

要实现此目标的步骤是什么?用我使用的所有功能原型制作头文件?以及将cpp和头文件与所有实际功能放置在哪里?有什么方法可以直接调用函数文件的目录?

What are the steps to accomplish this? Make a header file with all the function prototypes I used? And where to.place the cpp and header file with all the actually functions? Is there a way I can call the directory of the functions file directly?

即我更想将其与主要源cpp文件放在同一文件夹中,以便与我的一些同事共享.

I.e. I'm more thinking of having it in the same folder with the main source cpp file to share with some of my colleagues.

我该怎么做?

Windows.miniGW编译器

Windows. miniGW compiler

推荐答案

在标头中声明函数

//MyFunctions.h
int myFunction1(int,int);
int myFunction2(int,int);


在MyFunctions.cpp中实现它们


Implement them in MyFunctions.cpp

//MyFunctions.cpp
#include "MyFunctions.h"
int myFunction1(int a, int b){
    //your code
}
int myFunction2(int a, int b){
    //your code
}


将标题包含在所需的任何文件中


Include the header in whatever file you want

//OtherFile.cpp
#include "MyFunctions.h"
//Now you have access to the functions defined in MyFunctions.h in this file


我不知道miniGW,但是它看起来应该像g ++ otherfile.cpp MyFunctions.cpp ...


I dont know miniGW but it should look something like g++ otherfile.cpp MyFunctions.cpp ...

这篇关于如何从另一个.cpp文件中的一个.cpp文件调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆