gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:58:11 31 4
gpt4 key购买 nike

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

如果你不明白,让我解释一下:
我有一个文件:main.cpp在里面我有 main 函数。

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

有什么帮助吗?

最佳答案

您必须使用称为“标题”的工具。在标题中声明要使用的函数。然后将其包含在两个文件中。 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/6995572/

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