Filestream Problems

Hi everyone,

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
using namespace 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, const int &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

1
2
3
4
5
fstream getFilestreamValue(const string &filename)
{
   fstream tmp(filename.c_str());
   return tmp;
}


Any ideas? I'm using MinGW 3.4.x through CodeBlocks.
Last edited on
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));
Last edited on
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.
Ah, good to know. Thanks Bazzy and helios for the help!
Last edited on
Topic archived. No new replies allowed.