Thread: Constructor help!

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Constructor help!

    I have a class with a constructor, but everytime i run, it crashes, with a
    "raised exception...blah lah blah", erorr.

    Here is the code

    Code:
    #ifndef SHIPH 
    #define SHIPH 
    class Ship 
    { 
    public: 
    
    int health; 
    
    int x1; 
    
    int y1; 
    
    TImage *Ship_Sprite; 
    
    Ship(); 
    }; 
    
    Ship::Ship() 
    { 
    
    TImage *Ship_Sprite = new TImage(Form1); 
    
    health = 100; 
    
    Ship_Sprite->Picture->LoadFromFile("ship-1.bmp"); 
    
    x1 = 10; 
    
    y1 = 20; 
    } 
    
    #endif
    It crashes at the "TImage *Ship_Sprite = new TImage(Form1);" line.

    thx

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    A few things *bad* about the code - first, it isn't indented at all. Second, all the member variables of the Ship class are public - usually, you keep them as either protected or private, and either use accessor functions to change them or let them be used internally by the class' public functions. Third, this line:
    TImage *Ship_Sprite = new TImage(Form1);

    declares a new TImage* variable as a local variable, instead of initializing the member variable called Ship_Sprite. Just take out the "TImage *" part of the line to fix this particular problem.

    About why it's crashing, I can't say unless you post the code for the TImage. Also to note, you usually want to have a destructor if you have a constructor that does any dynamic memory allocation, in order to free the memory when the object is destroyed. In fact, if this particular code (the constructor) is running a LOT of times, without you ever freeing the memory you allocate, that could be the problem.

    Hope this helps
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    kk. about the indenting, it is the the source, but when i copied it got rid of the indent.

    TImage is a BCB thing, so i don't know the code.

    "use accessor functions to change them" how do i do this? i *want* them to be public!

    thx

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>"use accessor functions to change them" how do i do this?
    Well, what an accessor function is, is (for example):
    Code:
    class MyClass
    {
    public:
        void setMyNum(int newNum) {num = newNum;}
        int getMyNum() {return num;}
    protected:
        int num;
    };
    setMyNum and getMyNum are the accessor functions. You don't always have to do this, but that seems to be how most people do it. If you want a class with a bunch of public variables, usually you make it a struct - although if you need a constructor, then I guess you might as well do a class.
    let them be used internally by the class' public functions.
    What I meant by this, is (for example) in your class, it looks like the Ship is going to be drawn on the screen, moved around, etc. If that's the case, you might consider creating member functions for the class such as move(), draw(), etc. that change the x,y variables and whatever else - and make those variables protected instead of public.

    >>TImage is a BCB thing, so i don't know the code.
    Hm, ok. Then there's only a couple things I can think of that might be the problem.
    1. Maybe Form1 is invalid, i.e. if it's a pointer, maybe it's NULL or points to 'garbage', etc. then you might get a crash.
    2. As I said before, if Ship's constructor is getting called a bunch, maybe you're running out of memory because of the memory leak I mentioned.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed