Error: C2143: missing ';' before '.'

Hi. I am making a consle rpg, but I have a problem. I am getting "C2143: syntax error : missing ';' before '.'". The problem is in this line "hero.attribute [ATR_HEALTH] = 10;". I included the header file with the class. Sorry if it is a noob question. Can someone help me?
Here are the contents of the header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <map>

using namespace std;


class hero
{
	private:
		enum Attribute {ATR_HEALTH, ATR_MAXHEALTH, ATR_XP, ATR_MAXXP};
		map <Attribute, int> attribute;
	public:
		hero()
		{
			attribute [ATR_HEALTH] = 0;
			attribute [ATR_MAXHEALTH] = 0;
			attribute [ATR_XP] = 0;
			attribute [ATR_MAXXP] = 0;
		}
};
There isn't any problem with the code you have posted. You probably have a mistake in your main() function. So if you'd post the main code, I can try to help you further.
hero is the class name, not the object name.

If the issue is in main, do this:

1
2
hero myHero;
myHero.attribute[ hero::ATR_HEALTH ] = 10;


However, note that you'll get another error (cannot access private member).

ANother option is to make attribute static and public then do this:
hero::attribute[ hero::ATR_HEALTH ] = 10;
hero is the class name, not the object name.

If the issue is in main, do this:


1
2
hero myHero;
myHero.attribute[ hero::ATR_HEALTH ] = 10;



However, note that you'll get another error (cannot access private member).

ANother option is to make attribute static and public then do this:
hero::attribute[ hero::ATR_HEALTH ] = 10;


Thx! The first solution worked! The problem was in main.
Topic archived. No new replies allowed.