Re: I got a strange error message when using fstream!
On 31 Mrz., 11:31, Wader <WaderC...@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! ]