假设我们有两个文件:main.cpp和function.cpp。我们想要在main.cpp中使用function.cpp中的函数,可以使用include命令。

在function.cpp中定义一个函数:

// function.cpp

#include <iostream>

void printMessage() {
    std::cout << "Hello, World!" << std::endl;
}

在main.cpp中使用该函数:

// main.cpp

#include <iostream>
#include "function.cpp"

int main() {
    std::cout << "Starting program..." << std::endl;
    printMessage();
    std::cout << "Program finished." << std::endl;
    return 0;
}

在main.cpp中,我们使用include命令将function.cpp文件包含进来,然后就可以使用其中定义的函数了。注意,在include命令中,我们使用双引号将文件名括起来,表示该文件与当前文件在同一目录下。如果文件不在同一目录下,可以使用相对路径或绝对路径来指定文件路径。

用include写个列子

原文地址: https://www.cveoy.top/t/topic/buwD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录