简体   繁体   中英

Using fstream in struct, errors

I'm trying to initialize a file output stream inside a struct. The file that should be created has a name that i'd like to define in the main function, ie:

#include <iostream>
#include <fstream>

struct str {
std::ofstream fs;

};

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

This gives me: error: no match for call to '(std::fstream {aka std::basic_fstream}) (const char [10], const openmode&)'|

How to do it right?

The initialization constructor can't be used this way. Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM