calling member function from constructor

This is a discussion on calling member function from constructor within the C++ Programming forums, part of the General Programming Boards category; I have this code for a class called Character, If I initialize a character object my program breaks ( by ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    calling member function from constructor

    I have this code for a class called Character, If I initialize a character object my program breaks ( by calling constructor) ... but If i comment out the member function cut_sheet() then my program works as intended.

    any idea?

    Code:
    Character::Character()
    {
    	sheet_file = "sprites.png";
    	cut_sheet();
    	dir = DOWN;
    	frame = 0;
    	velocity_x = 0;
    	velocity_y = 0;
    }
    
    void Character::cut_sheet()
    {
    	for(int i=0;i<sizeof(ani_up)/sizeof(SDL_Rect*);++i)
    	{
    		ani_up[i].x = 55 + i*40;
    		ani_up[i].y = 320;
    		ani_up[i].w = 20;
    		ani_up[i].h = 30;
    	}
    	for(int i=0;i<sizeof(ani_down)/sizeof(SDL_Rect*);++i)
    	{
    		ani_down[i].x = 55 + i*40;
    		ani_down[i].y = 360;
    		ani_down[i].w = 20;
    		ani_down[i].h = 30;
    	}
    	for(int i=0;i<sizeof(ani_left)/sizeof(SDL_Rect*);++i)
    	{
    		ani_left[i].x = 55 + i*40;
    		ani_left[i].y = 400;
    		ani_left[i].w = 20;
    		ani_left[i].h = 30;
    	}
    	for(int i=0;i<sizeof(ani_right)/sizeof(SDL_Rect*);++i)
    	{
    		ani_right[i].x = 55 + i*40;
    		ani_right[i].y = 440;
    		ani_right[i].w = 20;
    		ani_right[i].h = 30;
    	}
    }
    And I am initializing it something like this in another file
    Code:
    Character *unit = new Character();
    Last edited by rodrigorules; 05-09-2010 at 08:57 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Calling a member function from a constructor is OK (assuming it doesn't try to use the members whose values you have not set yet).

    It is more likely that your cut_sheet is broken (e.g goes out of bounds). For example, perhaps you should be dividing by sizeof(*ani_up) etc (assuming it is a fixed-sized array).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yes its a fixed size array

    SDL_Rect ani_up[7];

    how would I do it correctly?

    O i see my problem, i am dividing by SDL_Rect*

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    for(int i=0;i<sizeof(ani_up)/sizeof(SDL_Rect);++i)
    //or
    for(int i = 0; i < sizeof(ani_up) / sizeof(*ani_up); ++i)
    //or even
    for (int i = 0; i < boost::size(ani_up); ++i) //safest of them all, you can also implement your own
    The size of the pointer to SDL_Rect is irrelevant to the calculation, because you have an array of SDL_Rects, not pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    nice,

    using boost from now on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Calling a member function from a different file
    By cdonlan in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2005, 11:50 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21