syntax error

I am in the midsts of making a battle system and have just added the ability to use skills. Unfortunately, every time I compile it, this happens and I cannot seem to figure out why. Please help.
Here is the error:
1
2
3
4
5
6
7
8
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(19): error C2143: syntax error : missing ';' before '.'
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(19): error C2086: 'Spells Player::ability1' : redefinition
1>          f:\nick\visual studio 2010\projects\project2\project2\player.h(18) : see declaration of 'Player::ability1'
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(19): error C2238: unexpected token(s) preceding ';'
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(20): error C2143: syntax error : missing ';' before '.'
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(20): error C2086: 'Spells Player::ability1' : redefinition
1>          f:\nick\visual studio 2010\projects\project2\project2\player.h(18) : see declaration of 'Player::ability1'
1>f:\nick\visual studio 2010\projects\project2\project2\player.h(20): error C2238: unexpected token(s) preceding ';'


Here's player.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
#ifndef Player_h
#define Player_h

#include "Enemy.h"
#include "main.h"
#include "Spells.h"

#include <iostream>
using namespace std;

class Player{
public: 
	string name;
	short health;
	short power;
	short defence;
	short magic; 
	Spells ability1;
	Spells ability1.name = "Apple Bomb";
	Spells ability1.MP = 5;
	
	Player(string newName, short newHealth = 100, short newPower = 15, short newDefence = 20, short newMagic = 30);

	void attack (Enemy& target);
	void display();
};
#endif 


here's the Spells.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef Spells_h
#define Spells_h

#include <iostream>
#include <string>
using namespace std;

struct Spells{
	string name;
	short MP;
};

#endif 


and...main.h:
1
2
3
4
5
6
7
8
9
10
#ifndef Main_h
#define Main_h

#include <string>

class Enemy;
class Player;
struct Spells;

#endif 


Please help me fix the problem and thank you for taking your time to help me.
It seems the compiler does not allow initialization of class members inside its definition

1
2
	Spells ability1.name = "Apple Bomb";
	Spells ability1.MP = 5;
you need to make it const, from what i know
It seems the compiler does not allow initialization of class members inside its definition

1
2
Spells ability1.name = "Apple Bomb";
Spells ability1.MP = 5;


How would I go about doing that?

you need to make it const, from what i know


For MP? That does make sense but I dont think that fixes the problem...
you cannot set variable data inside the class definition. You would have to do it in the constructor so move:
1
2
3
Spells ability1;
	Spells ability1.name = "Apple Bomb";
	Spells ability1.MP = 5;

into the constructor of Player in Player.cpp.
Last edited on
Thank you for all your help! It works now. :)
Last edited on
Topic archived. No new replies allowed.