Results 1 to 10 of 10

Thread: class problems!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Exclamation class problems!!

    Hi There,

    I have a class called person...

    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.

    Any help would greatly be appreciated!

    Thanks

    Regards,

    Smithy.
    Attached Files Attached Files

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The best idea is to throw an exception, since constructors cannot return values. For example:
    Code:
    class Thing {
    public:
        Thing(int x) {
            if(x > 5) throw "Too big";
        }
    };
    
    int main() {
        Thing *pThat;
    
        try {
            pThat = new Thing(6); // Throws exception
        } catch(char *pException) {
            cout << "Error: " << pException << endl;
            return -1;
        }
    
        cout << "Worked" << endl;
    
        return 0;
    }
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile thanks..

    Thanks.

    Just one final question, if I want to make 20 instances of the person class what's the best way to do it?

    is there a more effective way then just

    person person1;
    person person2;
    person person3;
    person person4;
    person person5;
    etc..


    Regards,

    Smithy.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    yup, make an array
    Code:
    person persons[20];
    indexes for this example will start at 0 and go to 19.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Thanks, i got one more quick question

    Thanks.

    How do I make a member function call that applies to all of the person arrays?

    i.e. how do i call person::something

    that will apply to all of the arrarys so i can access the private data from each array in the person::something member function.

    Thanks.

    Regards,

    Smithy.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    additonal info

    just to explain my problem a little furthur...

    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);


    Any help would greatly be appreciated!

    Thanks!

    Rgards,

    Smithy.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    thanks

    cool, thanks for that, if it's not too much trouble could u gives us a quick sample on how doing it with a pointer might work?

    thanks

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Thanks again

    Thanks again parksie

    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
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width