class person //define person class
{
int currentfloor;
int destfloor;
char name [10];
public:
//Come in creates 20 people instances
void come_in(void);
void exit(int num);
person:erson()
{
static int count = 0;
const int recordlength = 16;
//load a person from person.txt and store them
ifstream infile;
infile.open("person.txt");
if(!infile.fail())
{
cout << "initialising person " << count+1 << "..." << endl;
infile.seekg(recordlength*count++, ios::beg);
infile >> name >> currentfloor >> destfloor;
infile.close();
}
else
{
cout << "File not found..." << endl;
cout << "Fatal error..." << endl;
cout << "Terminating program..." << endl;
//How can I terminate the program from here??
}
}
};
My question is how can I terminate my program when there is an error in person:erson? i have tried to pass a number through person::exit
which changes elevator1.closed (elevator is another class in my program) however, the compiler says that it's an undeclared idetifier.
I've included my source in a zip file to show u what i mean in more detail.
void elevator:pen_doors(void)
{
//Check if people need to come aboard, if so aboard them.
cout << "Getting On : ";
for (int i = 0; i ==19; i++)
{
person::get_on(i,level);
}
}
note: level is a priv int in elevator.
I keep getting the compiler error:
error C2352: 'person::get_on' : illegal call of non-static member function
get_on in the person class is public and is defined as:
void get_on(int, int);
The class itself doesn't know how many instances there are, so you can't do this. And anyway, a static member function is not allowed access to the internal data because the compiler doesn't know which instance you mean.
How about passing a pointer to the array and seeing how that goes?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
Well, I'm not sure about the way you're going about it, design-wise, but here's one way...sorta ;-)
Code:
void elevator::open_doors(person *pPeople, int iCount) {
//Check if people need to come aboard, if so aboard them.
cout << "Getting On : ";
for (int i = 0; i < iCount; ++i) {
get_on(&(pPeople[i]));
}
}
void elevator::get_on(person *pPerson) {
cout << pPerson->some_member_data << " has just got on the elevator" << endl;
}
void somecode() {
elevator e;
person people[20];
e.get_on(people, 20);
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You". -- Linus Torvalds
However, I'm still a little lost with it. I have included an updated version of my code in this zip. If possible could you sorta have a look at this and tell me how to use the pointers a little better.
Thanks again for all of your help