Jump to content
  • Advertisement
Sign in to follow this  
Xabbu

Class/Function Problem

This topic is 5149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok.I have Player Client created in int main().(Player is the class).I have Client's stuff set with accessor methods. Then i have function Status() created before main. Status (is supposed to) print the player's stats,(which were set in main). so its like this:
//code
...
void Status()
{
 Client.GetHp();
// etc etc
}
int main()
{
//code
...
Player Client;
 Client.SetHp()
// etc etc
}


When i try and compile. I get an error in Status saying Client is not defined. How can i fix this?

Share this post


Link to post
Share on other sites
Advertisement
you say that Client is created in your main() function. so it is only visible to the main() function, when you try to call Client.GetHp() from your Status() function, it knows nothing about this Client object. If the error points to your call to client in your Status function, this is probably the problem.

Share this post


Link to post
Share on other sites
Yeah i know that. However my intelligence lacks the knowledge on how to fix this problem..

EDIT:Yay i fixed it. I declared Client globaly.

Share this post


Link to post
Share on other sites
Its a scope problem.
Either declare Client outside of main thus making it a global....

Player client;
int main()
{
}



.... which isn't the best practice. Or pass client as an argument...

int main()
{
Player Client;
Client.SetHp()
status(Client);
}
void status(Player& x)
{
x.GetHP();
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster

void Status(const Player& Client)
{
Client.GetHp();
}



Provided that 'void Player::GertHp() const' is declared. If not disregard const.

in your main call Status(Client) function.

Share this post


Link to post
Share on other sites
Dude, you implement the Player type so that any instance of Player does what Client can do, like so...

class Player
{
public:
void Status() const { cout << "My health is ==> " << health; }
};

int main()
{
Player client, other;
// set client, other hp
client.Status();
other.Status();
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!