STL Question not answered in FAQ
Gordon Neal
gneal@ichips.intel.com
Fri Mar 19 10:43: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. Here is an example of the code:
#include <stdio.h>
#include "stl.h"
class PtrWrapper {
int *a;
public:
PtrWrapper() {}
PtrWrapper(int *x):a(x) {}
~PtrWrapper() {}
PtrWrapper(const PtrWrapper& x) {}
PtrWrapper& operator= (const PtrWrapper& x) { }
};
class B {
friend class D;
list<PtrWrapper> ptr;
public:
B() {}
~B() {}
B(const B& x) {}
B& operator= (const B& x) { }
addPtr(int *a) {
ptr.push_back(PtrWrapper(a));
}
};
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) {}
C& operator= (const C& x) { }
};
class D {
vector<C> listc;
int i;
int j;
public:
D() {}
D(int sizec, int sizeb) {
int i;
listc.reserve(sizec);
for (i = 0; i < sizec; i++) {
listc.push_back(C(sizeb));
}
}
~D() {
}
D(const D& x) {}
D& operator= (const D& x) { }
print() {
int k = 0, l = 0;
vector<C>::iterator i;
vector<B>::iterator j;
printf("The size of vector c is %d\n",listc.size());
for (i = listc.begin(); i != listc.end(); i++) {
printf("The size of vector b for listc[%d] is
%d\n",k++,i->listb.size());
for (j = i->listb.begin(); j != i->listb.end() ; j++) {
printf("j = %d\n",l++);
}
}
}
};
main(int argc, char**argv) {
D d(10,20);
d.print();
}
The output of this:
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
The size of vector c is 10
The size of vector b for listc[0] is 0
The size of vector b for listc[1] is 0
The size of vector b for listc[2] is 0
The size of vector b for listc[3] is 0
The size of vector b for listc[4] is 0
The size of vector b for listc[5] is 0
The size of vector b for listc[6] is 0
The size of vector b for listc[7] is 0
The size of vector b for listc[8] is 0
The size of vector b for listc[9] is 0
The question is why is listb size 0? What am I doing wrong?
Thanks
More information about the Gcc
mailing list