From: David Pol on
On 31 mar, 11:31, Wader <WaderC...(a)gmail.com> wrote:
> #include <fstream>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> class MyObj {
> public:
> int data;
> ifstream in;
>
> };
>
> typedef std::vector<MyObj> MyObjVector;
>
> int main(int arc, char* argv[]) {
> MyObjVector myVector;
>
> MyObj obj;
> obj.data = 10;
> obj.in.open("test.txt");
>
> myVector.push_back(obj);
>
> }
>
> In the code above, I want to use a vector of class MyObj, which have a
> ifstream object, but I got a message says that I can't access the
> private memeber of ios_base, both in MinGW and Visual C++, but if a
> commented the line
> myVector.push_back(obj);
> all thing is OK, can't any one help me!

Hello,

In order to be stored in a std::vector<>, a type must meet the
requeriments of CopyConstructible and Assignable types. Streams do not
satisfy these requeriments, as the copy constructor and assignment
operator for std::basic_ios<> (a base class that provides common
functionality and state required by all streams) are private and not
defined.

Note that you do not define a copy constructor for MyObj. So, as soon
as you try to use one (in your case, by pushing back an instance of
MyObj into a std::vector<> (1), but obviously the compiler error would
be the same if you tried to construct a second instance of MyObj from
the original instance), the compiler tries to generate a copy
constructor for MyObj. And a compiler-generated copy constructor
simply copies each non-static data member of the source object over to
the target object, which in this case is not possible because
std::ifstream does not define a copy constructor (specifically, the
compiler tries to generate a copy constructor for
std::basic_ifstream<>, which in turn tries to call the copy
constructors of its base classes; the whole process ending up in a
rejected call to the private copy constructor declared in
std::basic_ios<>).

(1) Of course, defining a copy constructor for MyObj would not be
enough for this type to meet the mentioned requeriments to be stored
in a std::vector<>.

Regards,
David

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Oncaphillis on
Wader wrote:
> In the code above, I want to use a vector of class MyObj, which have a
> ifstream object, but I got a message says that I can't access the
> private memeber of ios_base, both in MinGW and Visual C++, but if a
> commented the line
> myVector.push_back(obj);
> all thing is OK, can't any one help me!

push_back tries to append a *copy* of MyObj into
the vector. So MyObj has to provide a copy constructor.
Since you did not provide a copy constructor.
C++ invokes it's own by trying to invoke the
copy constructor on all data members of MyObj.
This ends up into invoking a copy constructor on
ios_base (the base class of ifstream) which is
intentionally declared private.


So either you declare your own copy ctor on MyObj
which somehow deals with the 'copying' of ifstream
(meaning implementing your desired semantics) or
you kick out the ifstream).

I don't know what your intentions are to hold
a couple of ifstreams in a vector.

Hope that helps

O.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]