Hi
After getting great help with my first post here I thought I would try again. I need to create a (dynamic) number of objects of two types, eggs and fry. I then need to be able to delete the fry and then make a new group of fry objects with all the info from the members of the egg objects (i.e. the egg objects will become the new fry objects). the number of objects will vary so I cannot just over write the old values. I have some code that actually works, but as the program exits a Windows error box appears. I am thinking some sort of memory problem but I am stumped. My code is:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "class1.hpp"
#include "class2.hpp"
int main()
{
Stage1 *eggs;
eggs = new Stage1[3];
for(int i=0;i<3;i++)
{
cout << "Egg number " << i << " Age " << eggs.getAge() << " Weight " << eggs.getWeight() << endl;
}
Stage2 *fry;
fry = new Stage2[3];
cout << endl;
for(int i=0;i<3;i++)
{
cout << "Fry number " << i << " Age " << fry.getAge() << " Weight " << fry.getWeight() << endl;
}
delete [] fry;
cout << endl;
for(int i=0;i<3;i++)
{
Stage2 *fry;
fry = new Stage2;
fry.setAge( eggs.getAge() );
fry.setWeight( eggs.getWeight() );
cout << "New Fry number " << i << " Age " << fry.getAge() << " Weight " << fry.getWeight() << endl;
}
cin.get();
return 0;
}
The header files are:
class1.hpp
//********************************************
class Stage1
{
public:
Stage1();
~Stage1();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage1::Stage1():
itsAge(1),
itsWeight(5)
{
}
Stage1::~Stage1()
{
}
class2.hpp
//********************************************
class Stage2
{
public:
Stage2();
~Stage2();
int getAge() {return itsAge;}
void setAge (int Age) {itsAge = Age;}
int getWeight () {return itsWeight;}
void setWeight (int Weight) {itsWeight = Weight;}
protected:
int itsAge;
int itsWeight;
};
Stage2::Stage2():
itsAge(2),
itsWeight(9)
{
}
Stage2::~Stage2()
{
}
As I said, this works fine and I get the correct values, however on program exit I get an error box saying errors have been generated and the top of the log file looks like this:
Application exception occurred:
App: (pid=680)
When: 12/5/2003 @ 14:50:59.859
Exception number: c0000005 (access violation)
Please help
Fishman
PS I know this code could have been done better with derived or virtual classes but it is just to try to illustrate my problem