samedi 15 avril 2017

Insert instance of ofstream to container

I created my a class file which is a wrapper for std::ofstream. I created a Container to contain all the instances of file.

class file
{
private:
      std::ofstream ofs;

public:
      void open(std::string filename);
      void write(std::string s);
      void close();
};

class Container
{
private:
  std::map<int, file> m;


public:
      void insert(file f,int i)
      {
            m.insert(std::pair<int,file> (i,f));
      }
      void get(int i)
      {
            m.at(i);
      }
};

However, there is an issue in this code. In the insert method I am attempting to copy a std::pair<int,file> which cannot be done as the copy constructor of std::ofstream is deleted (see compilation error below).

I would like to successively add instances of file in a container. How can I do that?


Here is the compilation error

In file included from src/test.cpp:1:
In file included from /Applications/http://ift.tt/1CoLm4I:
In file included from /Applications/http://ift.tt/1rE7FEj:
In file included from /Applications/http://ift.tt/1FMh6ZU:
In file included from /Applications/http://ift.tt/XNllzo:
In file included from /Applications/http://ift.tt/10YFzYE:
/Applications/http://ift.tt/2pDk10E: error: call to implicitly-deleted copy constructor of
      'file'
        : first(__x), second(__y) {}
                      ^      ~~~
src/test.cpp:35:22: note: in instantiation of member function 'std::__1::pair<int, file>::pair' requested here
            m.insert(std::pair<int,file> (i,f));
                     ^
src/test.cpp:9:21: note: copy constructor of 'file' is implicitly deleted because field 'ofs' has a deleted copy constructor
      std::ofstream ofs;
                    ^
/Applications/http://ift.tt/2pjs5Hg: note: copy constructor is implicitly deleted because
      'basic_ofstream<char, std::__1::char_traits<char> >' has a user-declared move constructor
    basic_ofstream(basic_ofstream&& __rhs);
    ^
1 error generated.

Aucun commentaire:

Enregistrer un commentaire