Page 1 of 1

[SOLVED] My code is broken

Posted: Thu Mar 31, 2011 1:29 pm
by jaybee
Hey guys. So after a brief hiatus I've started working on my shoot em up once again. Once I started digging around in the code I realized I was going to have to restructure things quite a bit. What I'm working on right now is a ship manager class that will take care of drawing the player and enemy ships and what not. So anyways it works fine as far as the enemies are concerned and does what it supposed to do there, but, I can't get it to instantiate a player.

I'm sure I'm missing something totally trivial and easy here but I've been up extremely late the past few nights trying to get this all working and I can't for the life of me find the problem. heres what I'm trying to do:

Code: Select all

class ShipManager
{
     ....


public:
    Character player;

};
When I add the player object to the header code::blocks points at the constructor for my ShipManager class and spits out the following errors.

error: no matching function for call to 'Character::Character()'
note: candidates are: Character::Character(int, int, int, int, int, float, float, float)
note: Character::Character(const Character&)


I'm a little cracked out on my own code right now and I'm sure this is something simple but I just can't find it. Any clues? I can post any other code that may be applicable after I get off work.

Re: My code is broken

Posted: Thu Mar 31, 2011 1:53 pm
by adikid89
It's because of Character player; when you do that.. it'll call the Character() constructor.. the one with no parameters.. which you don't have.
What you can do is... in the initializer list initialize the player with the appropriate constructor, like so:

Code: Select all

ShipManager(): player(0,0,0,0,0.f,0.f) {}
Or make the player a pointer.. as such: Character* player; And you just new it when you need it with the appropriate constructor like this:

Code: Select all

player = new Character(0,0,0,0,0.f,0.f); 
. Just remember to free it when you're done.. like in the destructor or somewhere.

Re: My code is broken

Posted: Thu Mar 31, 2011 1:56 pm
by dandymcgee
jaybee wrote: When I add the player object to the header code::blocks points at the constructor for my ShipManager class and spits out the following errors.

error: no matching function for call to 'Character::Character()'
note: candidates are: Character::Character(int, int, int, int, int, float, float, float)
note: Character::Character(const Character&)
You overloaded the default constructor in your Character class to take 5 ints and 3 floats, so there is no longer a constructor which takes 0 arguments. This causes a line such as the following to fail:

Code: Select all

Character bob;
as well as

Code: Select all

Character* bob = new Character();
You either need to provide those arguments where you are constructing the object, or add a constructor that handles 0 arguments to your Character class:

Code: Select all

Character::Character();

Re: My code is broken

Posted: Thu Mar 31, 2011 2:00 pm
by jaybee
Ohhhh right thank you. Not sure how I missed that one. I've been kinda rewriting my code from the inside out and my brain is basically jello at this point. But hey I'm learning a lot in the process. Anyways thank you I really appreciate it.