Advertisement

classes

Started by July 04, 2000 06:05 PM
8 comments, last by Joker2000 24 years, 5 months ago
Why am I getting the following errors for this piece of code? class player { public: player(char *, int, int, int, int); void player_show_status(); void player_buy_weapon(int); void player_sell_weapon(); void player_buy_armor(int); void player_sell_armor(); int player_check_armor(); int player_take_armor_damage(); void player_buy_health(int); int player_check_health(); int player_take_health_damage(); void player_setup_account(int); int player_deposit_money(int); int player_withdraw_money(int); friend weapon; private: string name; int weapon_id; int armor; int health; int money; }; class weapon { public: weapon(char *, int, int, int); ~weapon(); void weapon_init_values(int); weapon_fire_weapon(int); void weapon_add_ammo(int); weapon_change_clip(int); void weapon_change_fire_method(); friend player; private: string name; int fire_method; int damage; int ammo; }; player p1; weapon w1; [C++Error] gunskilling.cpp(117): Could not find a match for ''player::player()''. [C++Error] gunskilling.cpp(118): Could not find a match for ''weapon::weapon()''. Thanks.
You don't have a constructor that takes no augments.

When you create your objects (p1 and w1 in this case) you have to pass the augments to the constructor:

eg:

player p1("Test", 1, 2, 3, 4);
weapon w1("Test", 1, 2, 3);


~Kavos

Edited by - Kavos on July 4, 2000 7:26:32 PM
Advertisement
What does that mean, does it mean you didnt define the constructor or something?, sorry for no help, I guess Id have to see all the code, sorry
Your problem is in your constructor.

You define player::player(char *, int, int, int, int) and then try to use player::player(void) which doesn''t exist.

The compiler can''t find a sutible constructor for player p1(player::player(void)) you either need to declare p1 as:

player p1("yourchar", yourint1, yourint2, yourint3, yourint4);

or put in an init member that takes(char *, int, int, int, int) and call it after declaring your player:

player p1; //player p1(); implied.
p1.init("yourchar", yourint1, yourint2, yourint3, yourint4);

You have the same problem with your weapon class.

Hope I helped and sorry if I''m confusing.



~Tim the Enchanter~
Arthur: Ohh great enchanter, tell us, what is your name?
Enchanter: Some call me Timmmmmm.
Arthur: Tell us great enchanter, how to you make fire without flint and tinder?
Enchanter: Silence!
Monty Python:The Search For The Holy Grail
Tim Wright
Spiffy Software
I'm not sure... it could be your compiler. It should create a default constructor for the classes.
When you put:
player p1;weapon w1;  


That means it tries to create those with a constructor that takes no arguments.
If instead if you put:
player p1('a',1,2,3,4);weapon w1('b',1,2,3,4);  


Then it could use the constructors you defined.
If you shoved a empty default constructor in there it should fix it too.

-----
I guess I was late posting a reply At least he got an answer!

Edited by - Atavist on July 4, 2000 7:29:38 PM
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Esap, if you don''t know the answer, why post?
Advertisement
Thanks guys...I think I''ve got everything solved. One more thing though. Ok, after the creation of an object (I guess it''s called an object):

player p1("Bobby",1,2,3,4); // is "p1" considered an object?


can I access other functions inside the player class by using the following syntax?:

p1.change_player_health(whatever params here);


and then the function itself would be defined like:
player::change_player_health(whatever params here);


Am I correct on all this?



Sounds right.

~Kavos
By the way, it''s pretty redundant naming all your functions with the ''Player_'' prefix, since you can only ever call those functions through a player object anyway.
Atavist: The compiler will not create a default constructor for your class if you have created a constructor. The reason for this is that when you create a constructor for your class that takes parameters, you are telling the compiler that is the only valid way to construct the object. In other words, you need the data passed to the constructor, or the object won''t work. If it makes sense to assign default data, then you should create a default constructor.

Example:


#include &ltstring>

class file
{
public:
file(string name) : name(name) {}

private:
string name;
};

void main()
{
file myfile; // but what name?
}



You can''t use a file without a name. It wouldn''t even make sense to provide a default file name. It''s better to have the file class trigger a compiler error if you have forgotten to pass the file name to the constructor, rather than trying to read from a file that doesn''t exist.

Note that you can''t create objects in an array without a default constructor (which is fine because the array wouldn''t be valid if its objects weren''t valid, and its objects wouldn''t be valid if their data was not valid).

If I skipped over anything or wrote poorly, please let me know - I''m in a hurry right now.

Good Luck!




- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement