gpt4 book ai didi

c++ - 在 C++ 程序中使用多个 .cpp 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:08 24 4
gpt4 key购买 nike

我最近从 Java 转向 C++,但现在当我编写我的应用程序时,我对编写 main 函数中的所有代码不感兴趣,我希望在 main 函数中调用另一个函数,但这个另一个函数在另一个 . .cpp 文件。

如果你不明白,让我解释得更好:
我有一个文件:main.cpp在里面我有主要功能。

我有第二个文件:second.cpp在里面我有一个函数叫做 second()我想将此函数称为 second()从我的主要功能..

有什么帮助吗?

最佳答案

您必须使用一种称为“ header ”的工具。在 header 中声明要使用的函数。然后将其包含在两个文件中。 header 是使用 #include 指令包含的单独文件。然后你可以调用另一个函数。

其他.h

void MyFunc();

main.cpp

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

其他.cpp

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

关于c++ - 在 C++ 程序中使用多个 .cpp 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com