I feel I am missing something...

Hello C++,

I have been using different tutorials over the past few days to achieve this terrible terrible code and I feel I am almost at the stage where it will work the way I want it to.. Except for one thing.

I have made my class called cPlayer and I can assign the values, but when I try to access these values from my BattleFunc, it gives me an error saying undeclared identifier.

I understand there is probably some gaping hole in my knowledge of C++ programming, but for all my googling I cant find a solution to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 class CClass { //////////////////////////////////////////////////////////////////////////////////From here
		int iStrength, iDexterity, iDefense, iHP, iCurHP; 
	public:
		void get_Values (int, int, int, int, int); 
		int Strength () {return (iStrength);}
		int Dexterity() {return (iDexterity);}
		int Defense() {return (iDefense);}
		int HP()		{return (iHP);}
		int CurHP()		{return (iCurHP);}
};
int main() {
	CClass cPlayer;
	cPlayer.get_Values(0,0,0,0,0);
	system("cls");
	cout << "Remaining Stat Points: " << iMaxStats << endl;
	cout << "Strength: "	<<	cPlayer.Strength()	<< endl;
	cout << "Dexterity: "	<<	cPlayer.Dexterity()	<< endl;
	cout << "Defense: "	<<	cPlayer.Defense()	<< endl;
	cout << "HP: "			<<	cPlayer.CurHP() << "/" << cPlayer.HP()<< endl; ////////////To here I have no problem with.
	BattleFunc();
}

void BattleFunc() {
	cout << cCow;
	cout << " appeared!\n";
	while(battling) {
		cout << "what will you do?\n 1:Attack\n 2:Flee\n";
		cin >> userInput;
		if(cPlayer.Dexterity >= iCowDex)	{        //Undeclared identifier error on cPlayer.
			if(userInput == 1) BattlePlayerAttackFunc();
			else if (userInput == 2) BattlePlayerFleeFunc();
			BattleEnemyAttackFunc();
			if(cPlayer.CurHP <= 0) BattlePlayerLoseFunc();
		}


(That isn't all of the code)

Hopefully I can find some help for this, if its possible at all :)

-Rob
Last edited on
BattleFunc doesn't know what cPlayer is. You'll need to do something like pass it in as an argument.
void BattleFunc( const CClass &cPlayer )

The call would then be...
BattleFunc( cPlayer );
My to cents:

cPlayer isn't a global variable, neither a BattleFunc local variable, so the compiler doesn't know where to find it. Try passing a CClass pointer as BattleFunc arguments and see what happen. If you don't know how to do it let me know. :)
Yeah, if you wouldn't mind explaining I would appreciate it :)
This is a problem of scope.

Variables exist only in the scope in which they're declared. They are "invisible" outside that scope. Scope is usually defined by {curly braces}.

For example:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    cout << "scope example\n";

    {  // adding curly braces here just for scope
        int var = 10;   // var is declared inside this scope

    } // var's scope ends here.  IE:  it no longer exists

    cout << var;  // ERROR, 'var' is undeclared
}


Your problem is similar, only instead of a nested scope like that example, you have it in a separate function:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    CClass cPlayer;  // cPlayer declared in this scope.

    //...
}  // <- cPlayer's scope ends here

// at this point, 'cPlayer' is no longer defined

void BattleFunc()
{
    //...
    if( cPlayer.whatever  ... // <- ERROR, cPlayer is undeclared 



Common solutions are:

1) Broaden the scope of cPlayer by defining it at a "higher level"... like globally. DON'T DO THIS, I am only mentioning it for completeness but it's a bad idea


2) Pass cPlayer to functions that need it as a function parameter. iHutch105's example illustrates how to do this.
Ok your function needs a definition of cPlayer, because it declared in main function but not in BattleFunc. So you need to give at your BattleFunc the variable that he need, ok?

You can do it in two ways, with a pointer or a reference. Let's try with a reference (how iHutch seggested you):

your function will be:
void BattleFunc( const CClass &cPlayer )

so you are saying that your function needs an argument of CClass type.

when you call this function you'll do this:

BattleFunc( cPlayer );

in this way you can operate on cPlayer variable inside your BattleFunc.

Here more info about functions argument :)
http://www.cplusplus.com/doc/tutorial/functions/
I had a read of the Functions(I) page, and tried the solution suggested, but the same debug error is occurring.

Ill list more of the code, if it helps :S


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
///////////Battle Functions/////////////
void BattleFunc( const CClass &cPlayer);


class CClass { //create class
		int iStrength, iDexterity, iDefense, iHP, iCurHP; // objects in the class
	public:
		void get_Values (int, int, int, int, int); //function to retrieve values
		int Strength () {return (iStrength);}
		int Dexterity() {return (iDexterity);}
		int Defense() {return (iDefense);}
		int HP()		{return (iHP);}
		int CurHP()		{return (iCurHP);}
};

int main() {
	CClass cPlayer;
	cPlayer.get_Values(0,0,0,0,0);
	system("cls");
	cout << "Remaining Stat Points: " << iMaxStats << endl;
	cout << "Strength: "	<<	cPlayer.Strength()	<< endl;
	cout << "Dexterity: "	<<	cPlayer.Dexterity()	<< endl;
	cout << "Defense: "	<<	cPlayer.Defense()	<< endl;
	cout << "HP: "			<<	cPlayer.CurHP() << "/" << cPlayer.HP()<< endl;
	BattleFunc(cPlayer);
}


void BattleFunc() {
	cout << cCow;
	cout << " appeared!\n";
	while(battling) {
		cout << "what will you do?\n 1:Attack\n 2:Flee\n";
		cin >> userInput;
		if(cPlayer.Dexterity >= iCowDex)	{
			if(userInput == 1) BattlePlayerAttackFunc(cPlayer);
			else if (userInput == 2) BattlePlayerFleeFunc();
			BattleEnemyAttackFunc(cPlayer);
			if(cPlayer.CurHP <= 0) BattlePlayerLoseFunc();
		}
		else {
			BattleEnemyAttackFunc(cPlayer);
			if(iCurCowHP <= 0){
				BattlePlayerWinFunc();
				battling = 0;
			}
			if (userInput == 1) BattlePlayerAttackFunc(cPlayer);
			else if (userInput == 2) BattlePlayerFleeFunc();
			}
		}
	return;	
}
Line 29 should be:

void BattleFunc(const CClass & cPlayer) {

Just changing the declaration isn't enough. The definition should match the declaration.
Last edited on
Cheers for the help guys, that seems to have helped.

Ill mark as solved, but if anyone else views this, any thoughts on.

"1>bad.cpp(175): error C2106: '-=' : left operand must be l-value"

1
2
3
4
5
6
7
8
void BattleEnemyAttackFunc(CClass &cPlayer) {
	int iTotalAttack = iCowStr + (10 / iCowDex) - (cPlayer.CurHP() + cPlayer.Defense() / 2);
	if(iTotalAttack >= 0) {
		cPlayer.CurHP() -= iTotalAttack;  // <<175
	}
	return;
}
}


Thanks again though :)
Hi,

cPlayer.CurHP() is a member function which only returns the HP, it doesn't set it.

You could add another member function called SetCurHp then you would call:
cPlayer.SetCurHp(cPlayer.CurHP() - iTotalAttack);

or if it were me I would add a cPlayer.TakeDamage() function and pass the damage into it

so it would be cPlayer.TakeDamage(iTotalAttack);

and then TakeDamage() would be something like:

void CClass ::TakeDamage(int damage =0)
{
iHP -= damage
}

Thanks -Anthony
Last edited on
Topic archived. No new replies allowed.