Using multiple .cpp files in c++ program?

前端 未结 4 2092
时光说笑
时光说笑 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:16

    You must use a tool called a "header". In a header you declare the function that you want to use. Then you include it in both files. A header is a separate file included using the #include directive. Then you may call the other function.

    other.h

    void MyFunc();
    

    main.cpp

    #include "other.h"
    int main() {
        MyFunc();
    }
    

    other.cpp

    #include "other.h"
    #include 
    void MyFunc() {
        std::cout << "Ohai from another .cpp file!";
        std::cin.get();
    }
    

提交回复
热议问题