【问题标题】:Using fstream in struct, errors在结构中使用 fstream,错误
【发布时间】:2013-12-10 14:57:53
【问题描述】:

我正在尝试在结构中初始化文件输出流。应该创建的文件有一个我想在主函数中定义的名称,即:

#include <iostream>
#include <fstream>

struct str {
std::ofstream fs;

};

int main() {
    str G;
    G.fs("hello.txt",std::ios::app);
}

这给了我: 错误:不匹配调用 '(std::fstream {aka std::basic_fstream}) (const char [10], const openmode&)'|

怎么做才对?

【问题讨论】:

    标签: c++ constructor fstream


    【解决方案1】:

    初始化构造函数不能这样使用。试试这个:

    int main() {
        str G;
        G.fs.open("hello.txt",std::ios::app);
    }
    

    【讨论】:

    • 我很高兴!如果这有帮助,请考虑对答案进行投票并将其标记为“已接受”:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多