STL Question not answered in FAQ
Joe Buck
jbuck@Synopsys.COM
Wed Mar 31 23:46:00 GMT 1999
> I am trying to use STL in the latest egcs release and have encountered a
> problem that seems fairly simple but I have yest to figure out why my code
> doesn't work.
It's because you defined a broken copy constructor in class C:
> class C {
> friend class D;
> vector<B> listb;
> public:
> C() {}
> C(int size) {
> int i;
> listb.reserve(size);
> printf("Reserved %d for list B\n",size);
> for (i = 0; i < size; i++) {
> listb.push_back(B());
> }
> }
> ~C() {}
> C(const C& x) {} // HERE.
> C& operator= (const C& x) { }
> };
Then when you say
> listc.push_back(C(sizeb));
The copy constructor is used to create the C objects in listc.
The resulting C object will have an empty vector.
DO NOT attempt to use the STL to store objects that do not meet the
requirements. At minimum you need a copy constructor that does a
correct copy; for some algorithms you also need an assignment operator
that does a correct assignment (here your assignment operator is
also broken).
More information about the Gcc
mailing list