So I've got a FileManager class that is being used to handle all requests for std::fstream objects. The open streams are stored in a map with the filename as a key, and the actual creation function goes like this:
1 2 3 4 5 6 7 8 9
usingnamespace std;
/*@param mode: a project-specific file mode
which is later translated into the ios_base::openmode type
by function getOpenMode().*/
fstream &getFilestream(const string &filename, constint &mode)
{
fileStreamMap[filename] = fstream(filename.c_str(),getOpenMode(mode));
return fileStreamMap[filename];
}
The problem is that I get a compile-time error that states error: `std::ios_base::ios_base(const std::ios_base&)' is private . I even get this from a plain old instantiation, such as
Streams don't have copy constructor nor assignment operator so you can't have streamA = streamB.
You can use a map of pointers to fstream and dynamically allocate them: fileStreamMap[filename] = new fstream(filename.c_str(),getOpenMode(mode));
Fragment 1, line 7: calling std::fstream's copy constructor, which is private. Make fileStreamMap an std::map<std::string,std::fstream *>
Fragment 2, line 4: calling std::fstream's copy constructor, which is private. Return a pointer to a dynamically allocated object.