Using multiple .cpp files in c++ program?

前端 未结 4 2091
时光说笑
时光说笑 2020-11-28 04:05

I recently moved from Java for C++ but now when I am writing my application I\'m not interested in writing everything of the code in the main function I want in main functio

4条回答
  •  时光取名叫无心
    2020-11-28 04:18

    You should have header files (.h) that contain the function's declaration, then a corresponding .cpp file that contains the definition. You then include the header file everywhere you need it. Note that the .cpp file that contains the definitions also needs to include (it's corresponding) header file.

    // main.cpp
    #include "second.h"
    int main () {
        secondFunction();
    }
    
    // second.h
    void secondFunction();
    
    // second.cpp
    #include "second.h"
    void secondFunction() {
       // do stuff
    }
    

提交回复
热议问题