簡體   English   中英

在結構中使用fstream,出現錯誤

[英]Using fstream in struct, errors

我正在嘗試在結構內部初始化文件輸出流。 我應該在主函數中定義的名稱應該創建的文件,即:

#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&)'|沒有匹配項

怎么做對?

初始化構造函數不能以這種方式使用。 嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM