Error undeclared identifier - classes

I've recently decided to make the move from Java to C++ I thought I would make a simple text-based game to get used to the language. Now I have declared my variables in my header file, but I'm unable to use them and am getting the Following "Error 14 error C2065: '....' : undeclared identifier
" for all of my variables.


Entity.h
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
#ifndef ENTITY_H
#define ENTITY_H
#include <string>
using namespace std;

class Entity
{
public:
	//set vars
	void setName(int ammount);
	void setMaxHealth(int ammount);
	void setHealth(int ammount);
	void setGold(int ammount);
	void setWeaponDamage(int ammount);
	void setArmorMultiplyer(int ammount);
	//get vars
	string getName();
	int getMaxHealth();
	int getHealth();
	int getGold();
	//actions
	void resetHealth();
	void takeDamage(int ammount);
	int doDamage();
protected:
	string name;
	int maxHealth;
	int health;
	int gold;
	int baseDamage;
	int weaponDamage;
	int armorMultiplyer;
};
#endif 



Entity.cpp
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
#include "stdafx.h"
#include "Entity.h"


//set vars
void setName(string text){
	name = text;
}
void setMaxHealth(int ammount){
	maxHealth = ammount;
}
void setHealth(int ammount){
	health = ammount;
	if (health > maxHealth){
		health = maxHealth;
	}
}
void setGold(int ammount){
	gold = ammount;
}
void setWeaponDamage(int ammount){
	weaponDamage = ammount;
}
void setArmorMultiplyer(int ammount){
	armorMultiplyer = ammount;
}
//get vars
string getName(){
	return name;
}
int getMaxHealth(){
	return maxHealth;
}
int getHealth(){
	return health;
}
int getGold(){
	return gold;
}
//actions
void resetHealth(){
	health = maxHealth;
}
void takeDamage(int ammount){
	health -= (ammount * armorMultiplyer);
}
int doDamage(){
	return (baseDamage + weaponDamage);
}

Last edited on
In Entity.cpp you define many functions which have no relation to your Entity class. You need to use the scope resolution operator to let the compiler know you are implementing the functions from the scope of your class:
6
7
8
void Entity::setName(string text){
	name = text;
}
Note that C++ also supports inline class definition with similar syntax to Java.
Last edited on
Thanks For your help!
Topic archived. No new replies allowed.