How to call functions from one .cpp file in another .cpp file?

前端 未结 4 1196
迷失自我
迷失自我 2021-02-06 06:55

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

Basically I have multiple cpp files with all my functions I made for use with binary trees, B

4条回答
  •  旧时难觅i
    2021-02-06 07:26

    That is quite simple, as JMAA said you should do some research to understand these concepts, but being practical, this is what you would do:

    You'll define an functionsExample.cpp where you have to define all your functions and also an functionsExample.h where you'll declare your functions.

    You'll have this as the functionsExample.cpp:

    #include 
    int example(int x, int y)
    {
        return x + y;
    }
    

    Ans this as functionsExample.h:

    #ifndef FUNCTIONSEXAMPLE_H
    #define FUNCTIONSEXAMPLE_H
    
    int example(int x, int y);
    
    #endif 
    

    Then in the cpp you want to run the example function, simply add:

    #include "functionsExample.h"
    

    But as I said, you should do some research about header guards, preprocessor directives and files organization to have a deeper knowledge about this. Some links I would recommend:

    Header Files

    Preprocessor Directives

提交回复
热议问题