Sign in to follow this  

Class/Function Problem

This topic is 4840 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
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
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

This topic is 4840 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this