使用 ios_base 和 ofstream 时出现 C++ 错误

标签 c++ file std

void DataSaver::save( const std::vector<long>& vec )
{
    using std::ios_base;
    std::ofstream file;
    file.open( "/home/david/Desktop/test", ios_base::out | ios_base::binary );
    for(int i = 0; i < vec.size(); i++)
        file << vec.at( i ) << "\00\01\10\00";
    file.close();
}

这会导致一些错误:

g++ main.cpp datasaver.cpp
datasaver.cpp: In member function ‘void DataSaver::save(const std::vector&)’:
datasaver.cpp:11:16: error: aggregate ‘std::ofstream file’ has incomplete type and cannot be defined
datasaver.cpp:12:41: error: incomplete type ‘std::ios_base’ used in nested name specifier
datasaver.cpp:12:57: error: incomplete type ‘std::ios_base’ used in nested name specifier

我包含了 iostreamofstreamvector

谁能解释一下哪里出了问题?

最佳答案

你可能需要

#include <fstream>

因为文件流实际上是在其中定义的

关于使用 ios_base 和 ofstream 时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469521/

相关文章:

c++ - 局部表面拟合到 3d 点

file - 如何在VIM中用当前文件名替换文本?

c++ - 如何更改 C++'11 中指针的所有权?

java - 访问共享上的文件(文件服务器)

python - 是否可以在 Python 类定义中使用导入的类方法而不运行导入文件中的所有代码?

c++ - std::map:当元素不可默认构造时创建/替换元素

C++ std::map 与动态数组

c++ - 赋值运算符中的内存泄漏

c++ - 子类编辑控件接受点,尽管它不应该接受

c++ - 将 regex_search 与 std::string 一起使用时的无限循环