Constructor Overloading

This is a discussion on Constructor Overloading within the C++ Programming forums, part of the General Programming Boards category; Folks, I'm having a strange problem where I can't seem to get my program recognise my overloaded constructors. I've declared ...

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    Constructor Overloading

    Folks,

    I'm having a strange problem where I can't seem to get my program recognise my overloaded constructors. I've declared 2 constructors, one with 6 parameters and then another with 8 parameters as below:-
    Code:
    SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x = 1, float y = 1, int i_lives = 1); //constructor
    
    SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x = 1, float y = 1, int i_lives = 1, bool frozen = true, double frozen_end_time = 0);
    
    SPRITE::SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x, float y, int i_lives)
    {
    	draw_area = draw_engine_ptr;
    	pos.x = x;
    	pos.y = y;
    	sprite_index = s_index;
    	num_lives = i_lives;
    
    	facing_dir.x=1;
    	facing_dir.y=0;
    
    	classID = SPRITE_CLASSID;
    
    	level_ptr = l_ptr;
                    frozen = false;
    	frozen_end_time = 0;
    
    }
    
    SPRITE::SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x, float y, int i_lives, bool new_frozen, double new_frozen_end_time)
    {
    
    	draw_area = draw_engine_ptr;
    	pos.x = x;
    	pos.y = y;
    	sprite_index = s_index;
    	num_lives = i_lives;
    
    	facing_dir.x=1;
    	facing_dir.y=0;
    
    	classID = SPRITE_CLASSID;
    
    	level_ptr = l_ptr;
    	frozen = new_frozen;
    	frozen_end_time = new_frozen_end_time;
    }
    Now I've got several classes that inherit from this SPRITE class and the majority of them only need to pass into the SPRITE constructor with 6 parameters like below:-

    Code:
    class CHARACTER : public SPRITE
    {
    
    public:
    
    	CHARACTER(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x = 1, float y = 1, int lives = 3,
    		char up_key = 'w', char down_key = 's', char left_key = 'a', char right_key = 'd');
    
    protected:
    	
    	char up_key;
    	char down_key; 
    	char left_key;
    	char right_key;
    };
    
    CHARACTER::CHARACTER(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, float x, float y, int lives,
    		char up, char down, char left, char right)
    		: SPRITE(l_ptr, draw_engine_ptr, s_index, x, y,lives)
    {
    	
    	up_key = up; 
    	down_key = down; 
    	left_key = left; 
    	right_key = right;
    
    	classID = CHARACTER_CLASSID;
    }
    
    };
    Now I'm explictly calling the SPRITE constructor that has 6 parameters but I keep getting the error:-

    error C2668: 'SPRITE::SPRITE' : ambiguous call to overloaded function

    Isn't it possible to overload constructors with different parameters ? I've re-checked to ensure that I'm passing in the correct parameters type's etc and all looks good.

    Any ideas ?

    Cheers
    Starkhorn

  2. #2
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    hmm. think I got it. Both my constructors had the exact same first 6 parameters which I think made it impossible for the compiler to know which constructor to call.

    I changed my 2nd constructor to:-

    Code:
    SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index, bool frozen = true, double frozen_end_time = 0, float x = 1, float y = 1, int i_lives = 1);
    
    SPRITE::SPRITE(LEVEL *l_ptr, DRAW_ENGINE *draw_engine_ptr, int s_index,  bool new_frozen, double new_frozen_end_time, float x, float y, int i_lives)
    {
    
    	draw_area = draw_engine_ptr;
    	pos.x = x;
    	pos.y = y;
    	sprite_index = s_index;
    	num_lives = i_lives;
    
    	facing_dir.x=1;
    	facing_dir.y=0;
    
    	classID = SPRITE_CLASSID;
    
    	level_ptr = l_ptr;
    	frozen = new_frozen;
    	frozen_end_time = new_frozen_end_time;
    }
    And it compiled without those errors.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The problem was caused by the default values. Because the 2 constructors had the same arguments and last couple defaulted values the compiler couldn't pick between them.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

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