Vector of Object Vectors

Hi,
I am trying to define a class which has a vector of objects of another class which in turn has a vector as a member variable. Please find the code below.
class subClass {
int size;
std::vector<int> testInt;
public:
subClass();
subClass(int);
virtual ~subClass();
void printVec(void);
};
subClass::subClass() {}
subClass::subClass(int x) {
size = x;
testInt.reserve(x);
cout<<"PRINT2.7: "<<size<<" "<<testInt.size()<<endl;
for (int i = 0; i < x; i++)
testInt.push_back(i*5);
cout<<"PRINT2.8: "<<size<<" "<<testInt.size()<<endl;
}
subClass::~subClass() {}
void subClass::printVec(void)
{
cout<<"PRINT2.9: "<<size<<" "<<testInt.size()<<endl;
for (int j = 0; j < testInt.size(); j--)
{
cout<<"JPRINT"<<j<<endl;
cout<<testInt.at(j)<<endl;
}
}

class newClass {
int size;
std::vector<subClass> testSub;
public:
newClass();
newClass(int);
virtual ~newClass();
void printVec(void);
};
newClass::newClass() {}
newClass::newClass(int x) {
size = x;
testSub.reserve(x);
for (int i = 0; i < x; i++)
testSub.push_back(subClass(i+1));

}
newClass::~newClass() {}
void newClass::printVec(void)
{
cout<<"PRINT1"<<endl;
for (int i = size - 1; i >= 0; i--)
{
cout<<"IPRINT"<<i<<endl;
testSub.at(i).printVec();
}
}

int main(int argc, char** argv) {

newClass noycles(1);// = new newClass(5);
cout<<"PRINT"<<endl;
noycles.printVec();

return 0;
}
The problem I am facing is that the size of the inner vector is always getting reset to zero. The output goes somewhat like this...
Output
PRINT2.7: 1 0
PRINT2.8: 1 1
PRINT
PRINT1
IPRINT0
PRINT2.9: 0 0
Press [Enter] to close the terminal ...

My aim is to print all the vectors. Can you please provide any clue as to what could be the possible reason for this behaviour?
I can't help you with that. I compiled and got
PRINT2.7: 1 0
PRINT2.8: 1 1
PRINT
PRINT1
IPRINT0
PRINT2.9: 1 1
JPRINT0
0
Which seems to be what you wanted. Could it be that the code you compiled is not the codeyou posted? There is no JPRINT too..
Thanks. I found the error. It is most probably a compiler issue. I compiled it using g++ 4.0 and it works with g++ 4.3 onwards.

PS: I did post the same code. The JPRINT is in the subClass.printvec() function.
Topic archived. No new replies allowed.