Skip to main content
gamedev.net
🔒 Locked

classes

Started by Joker2000 Jul 5, 2000 at 6:48 PM 6 replies 250+ views
Original Post
Joker2000
Joker2000
Here's my code so far with the problems: class player { public: player(string, int, int, int, int, int); ~player(void); void show_player_stats(void); private: string player_name; int weapon_status; int health; int armor; int money; int experience; }; void main(void) { string name; cout << "Enter player name:"; cin >> name; player p1(name, 0, 100, 0, 500, 0); // this next line causes error 1 player::show_player_stats(); } player::player(string p_name,int w,int h, int a, int m, int e) { this->player_name = p_name; this->weapon_status = w; etc etc etc } void player::show_player_stats(void) { // this line causes error 2 cout << p1.player_name << p1.weapon_status << etc. etc. etc. } ERRORS: Error 2: Undefined symbol 'p1'. Error 1: Use . or -> to call 'player::show_player_stats()'. Why the heck am I getting this error?! Of course it's not undefined! I freaking MADE it with "player p1(params)"! (Actually...I don't know if anything I did is right, I'm just yelling cause I'm frustrated and "think" it should work.) I tried declaring p1 globally by doing: player p1; at the very top, but then I get the error: Size of 'p1' is unknown or zero. Please help. Thanks PS: How can I get the name the user enters for "name" and get spaces as well? Because with "cin" it doesn't take spaces. Edited by - Joker2000 on 7/5/00 7:20:15 PM
loserkid
loserkid
Make p1 a global variable.

silly programmer vb is for kids.
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
Gladiator
Gladiator
Loser... Kid... you''re wrong...

Instead of having


void player::show_player_status(void)
{
cout << p1.player_name << p1.weapon_status << etc. etc. etc.
}

use something like this

void player::show_player_status(void)
{
cout << player_name << weapon_status << etc. etc. etc.
}

Note: You don''t need to do p1.player_name.. blah blah blah... just use the local data member of the class to display the status for that specific instance of the class...

Then in your main() function, instead of having

void main(void)
{
string name;
cout << "Enter player name:";
cin >> name;
player p1(name, 0, 100, 0, 500, 0);
show_player_status();
}

use the following...

void main(void)
{
string name;
cout << "Enter player name:";
cin >> name;
player p1(name, 0, 100, 0, 500, 0);
p1.show_player_status();
}

Hope this helps!

..-=ViKtOr=-..
Tim the Enchanter
Tim the Enchanter
Making p1 global could solve your problem, but would defeat the purpose of having a reuseable player class.

Your problem is in:

void player::show_player_status(void)
{
cout << p1.player_name << p1.weapon_status << etc. etc. etc.
}

The compiler doesn''t know what p1 is.

You can just say:

void player::show_player_status(void)
{
cout << player_name << weapon_status << etc. etc. etc.
}

because player_name and such are member variables in the player class.


By the way:

void player::show_player_status(void)
{
cout << player_name << weapon_status << etc. etc. etc.
}

implies:

void player::show_player_status(void)
{
cout << this->player_name << this->weapon_status << etc. etc. etc.
}

You don''t need to use the "this" keyword when your working in a class.

So you could say:

void player::player(string p_name,int w,int h, int a, int m, int e)
{
player_name = p_name;
weapon_status = w;
etc etc etc
}


Hope I helped.


~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
fenrir
fenrir
I could be wrong but why would you want to put that p1 there in the first place.
its a memberfunction
void player::show_player_status(void)

so you would call it with
p1.show_player_status();
and then the function alredy knows what player you mean

so you could use p2.show_player_status(); when you create a second player


hm this must be my crappiest explanation ever
anyway if you still don''t understand it, I''ll have an other go at it, just ask




fenrir
fenrir
hehe 3 replies in only 5 seconds, I''ll bet we have a record now
Joker2000
Joker2000
Ok, it works fine when I use p1.show_player_stats() in the main function, because that''s where I created p1. But, other functions, some inside the player class and some not in the player class, need to use p1 also. For example, on the main menu screen for the program, the user should be able to press "S" for the player stats. But the main menu screen is not located in main(). Therefore, main_menu() doesn''t know p1 exists and therefore I can''t call p1.show_player_stats() from my main_menu() function. Now that I''ve told you what I CAN''T do, can anyone tell me how it CAN work? Is this where I''d have to make p1 global? And how?
IO Fission
IO Fission
You could try sending p1 as a parameter to main_menu, either as a player class or a pointer to a player class.
Hope you can follow that


- IO Fission

Tearing 'em apart for no particular reason...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.