|
From: Wader on 30 Mar 2008 18:31 #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! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 31 Mar 2008 01:43 On 31 Mrz., 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! std::vector requires that any feasible element type (value type) must be CopyConstructible (and Assignable). This is a general Container requirement of the current C++ standard, see [lib.container.requirements]/3. The member in of type std::ifstream does not fulfill this requirement, which follows by implication, because it's base class basic_ios is not. One possible workaround for you might be to use a boost::shared_ptr<ifstream> instead and to use the free store ("new") to allocate the stream. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 31 Mar 2008 01:43 Wader wrote: > class MyObj { > public: > int data; > ifstream in; > }; > > typedef std::vector<MyObj> MyObjVector; std::vector requires that its elements are copyable and assignable. Since iostreams are neither copyable nor assignable, objects containing them aren't either (at least not unless you redefine these operations). From that follows that you can't use MyObj as element type of std::vector. > 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 Two things: 1. It is typically a good idea to quote error messages. 2. The copy constructor is declared private in order to prevent copying, that's why you get the error message. Uli -- Sator Laser GmbH Gesch�ftsf�hrer: Michael W�hrmann, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 31 Mar 2008 20:13 Wader 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! You can't copy stream objects. Think about it - what does it mean to make a copy of a file stream, does it create a new file, or what? To stop you from doing this, one of the base classes of all the streams has its copy constructor and assignment operator made private. Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Olivier Langlois on 31 Mar 2008 20:15
> 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! It is because ifstream is not copyable and one STL containers requirement for contained type is that they can be copyable with the copy constructor and/or the assignment operator. The default and the copy constructors + assignement operator are automatically generated by the compiler if they are not explicitly declared in a user defined class (like in your MyObj class). The only exception where the compiler will not generate those functions is if generating them would create an error. However, if in your code you still try to copy an object, the compiler will give you the reason why it cannot create the copy constructor. Mix that with the fact the methods of a class template is not instantiated before they are used in your code and this totally explain the behavior that you are experiencing. An additionnal interesting observation is that the reason why the compiler does not take the same liberty to optimize out unused automatically generated functions like it does for methods of class templates, it is that templates usage is totally determined at compile- time where as you could compile and store an ordinary class into a library and link with it at a later time. When the compiler compiles the ordinary class, it has no idea whether or not the automatically generated functions will be needed so it always generates them, if possible of course. Greetings, Olivier Langlois http://www.olivierlanglois.net http://blog.olivierlanglois.net -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |