Sign in to follow this  

loading w/ C++ error

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

Im working on a little hobby text rpg. I have a save and a load function for this and it works but now that im trying implement it. The save implemented correctly but the load doesn't.
	cout << "Would you like to load a saved character? y/n";
	cin >> answer;
		if (answer == 'y')
			player->Load();
it results in the error
'player' was not declared in this scope
Any ideas? My functions are all included in a library that is linked to this file through headers.

Share this post


Link to post
Share on other sites
It means exactly what it says. The variable 'player' has not been declared in the current scope. These words are perfectly normal English words; they're used only slightly metaphorically in programming.

Basically, what is 'player' supposed to be and where does it come from?

Share this post


Link to post
Share on other sites

player->SetHealth(player->GetMaxHealth());
player->SetMana(player->GetMaxMana());
cout << "HP: " << player->GetHealth() << " / " << player->GetMaxHealth() << " ";
cout << "MP: " << player->GetMana() << " / " << player->GetMaxMana() << "\n" ;
player->Save();

works just fine though in the same project.

Both have #include "Library.h" which links to my void PLAYER::Save() and PLAYER::Load() functions

Share this post


Link to post
Share on other sites
Just because it is in the same project does not necessarily mean the scope of the variables is always active.


//demo
int a = 0;
{
int b = 1;
cout << a << b;
}
//displays 01
cout << a << b;
//error b is no longer in scope due to }

Share this post


Link to post
Share on other sites
say you have this.



int globaldata;

void Function1()
{
int data1;
}

void Function2()
{
int data2;
}

main()
{
int data3;

data1 = 1; // not in the scope of main and will fail
data2 = 1; // not in the scope of main and will fail
globaldata = 1; // is in the scope of this current file and will be successfule
data3 = 1; // is in the scope of main() and will be successful
}





If you created the data in one function but are trying to use it in annother it will be out of scope. If you need more help post your code that contains the variables that have the errors.

Share this post


Link to post
Share on other sites
Quote:
Original post by Denzin
Functions included in the same header do not have sharing scope. You must declare your variable globally or pass it to the function.


Okay, I believe I understand, thanks. Ill tweek around with it after work and come back with any problems. Thanks for the lessons in scope.

Share this post


Link to post
Share on other sites

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