gpt4 book ai didi

c++ - 如何在头文件c++中定义ofstream变量

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

我有不止一个源文件和头文件。他们是;

main.cpp tools.cpp hh.cpp fitness.cpp

tools.h hh.h fitness.h

我想在 tools.h 中定义一个 ofstreamtools.h 包含在所有其他源文件中。但是当我将 std::ofstream myOutput; 添加到 tools.h 时,它会显示“myOutput 的多重定义”。但是,没有关于 myOutput 变量的任何其他行。如果我能正确定义它,那么我想执行 myOutput.open("observe.txt",std::ofstream::app);

最佳答案

extern std::ofstream myOutput;在 tools.h 和 std::ofstream MyOutput;在 tools.cpp 中。另外,放 #include <fstream>在 tools.cpp 中。不使用 std::输入行 using namespace std;在两个文件中。打开文件的代码在 main.cpp 中。

关于c++ - 如何在头文件c++中定义ofstream变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28406915/

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