Only constructors take member intializers

I'm working on a code for my Computer Science class and in this project we have to make a knight jousting game. But in the game we have to have two separate classes one for the knight and the other for the weapon. I have to use my weapon class in my knight class though and things just get really weird so I'll post my code and the error I keep getting exactly from the compiler because I'm confused on what to do.

Weapon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include"random.h"

using namespace std;

class Weapon
{
	public:
		void weaponStats(string type, int HitChance, int StaminaRequired);
	private:
		string Type;
		int Hit_Chance;
		int Stamina_Required;

};


Knight.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<string>
#include"weapon.h"

using namespace std;

class Knight
{
	public:
		void setKnightStats(string Knightname, int KnightStamina, string type, int HitChance, int StaminaRequired);
		void display();
	private:
		string KnightName;
		int Knight_Stamina;
		bool on_Horse;
		Weapon wield;


};

Knight.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include"Knight.h"
//#include"weapon.h"
#include<iostream>

using namespace std;

//Knight1 name
void Knight::setKnightStats(string Knightname, int KnightStamina, string type, int HitChance, int StaminaRequired)
: wield(type, HitChance, StaminaRequired)
{
	KnightName = Knightname;
	Knight_Stamina = KnightStamina;
	
}

void Knight::display()
{
	cout << KnightName << endl;
	cout << Knight_Stamina << endl;
}


p02.cpp //contains main
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
#include<iostream>
#include"Knight.h"
//#include"weapon.h"

using namespace std;

int main()
{
	Knight k1;
	Knight k2;
	Weapon w1;
	Weapon w2;


	//Knight1 Code
	string KnightName;
	int Stamina;
	int HitChance;
	string type;
	int staminaRequired;

	cout << "Player 1, Enter your knights name: ";
	cin >> KnightName;
	cout << KnightName << " enter your stamina: ";
	cin >> Stamina;
	cout << "What weapon are you wielding: ";
	cin >> type;
	cout << "What's the " << type << "'s hit chance? ";
	cin >> HitChance;
	cout << "What's the " << type << "'s stamina required: ";
	cin >> staminaRequired;
	k1.setKnightStats(KnightName, Stamina, type, HitChance, staminaRequired);

	//Knight2 Code
	string KnightName2;
	int Stamina2;
	int HitChance2;
	string type2;
	int staminaRequired2;

	cout << "PLayer 2, Enter your knights name: ";
	cin >> KnightName2;
	cout << KnightName2 << " enter your stamina: ";
	cin >> Stamina2;
	cout << "What weapon are you wielding: ";
	cin >> type2;
	cout << "What's the " << type2 << "'s hit chance? ";
	cin >> HitChance2;
	cout << "What's the " << type2 << "'s stamina required: ";
	cin >> staminaRequired2;

	k2.setKnightStats(KnightName2, Stamina2, type2, HitChance2, staminaRequired2);
}


and the error I keep on getting is this

$ g++ p02.cpp knight.cpp
knight.cpp: In member function ‘void Knight::setKnightStats(std::string, int, std::string, int, int)’:
knight.cpp:9:3: error: only constructors take member initializers
: wield(type, HitChance, StaminaRequired)
Last edited on
wield is an instance of Weapon class. The weapon class has only the default constructor (no arguments). You need to define a constructor that takes a string and two ints
So make a weapon.cpp and define my constructor in there? kind of like this

1
2
3
4
5
6
7
8
9
10
11
#include"weapon.h"
#include<iostream>

using namespace std;

void Weapon::weaponStats(string type, int HitChance, int StaminaRequired)
{
	Type = type;
	Hit_Chance = HitChance;
	Stamina_Required = StaminaRequired;
}
weaponStats is not a constructor, a constructor has the same name as the class..

u need to make a constructor for weapon and another one for Knight then move the initializer there :

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
class Weapon
{
public :
    Weapon( string Type, int Hit_Chance, int Stamina_Required )
    // member initializer list :
    : Type( Type ),
      Hit_Chance( Hit_Chance ),
      Stamina_Required( Stamina_Required )
    { /* body */ }

    // ...
private :
    string Type;
    int Hit_Chance;
    int Stamina_Required;
};

class Knight
{
public :
    Knight( string Knightname, int KnightStamina, string Type, int HitChance, int StaminaRequired )
    // member initializer list
    : KnightName( Knigtname ),
      Knight_Stamina( KnightStamina ),
      on_horse( ... ),
      wield( Type, HitChance, StaminaRequired )
    { }
    
    // ...
private :
    string KnightName;
    int Knight_Stamina;
    bool on_Horse;
    Weapon wield;
};


Constructors are much better than member functions that "sets" or "initializes" members like setKnightStats()
Last edited on
Topic archived. No new replies allowed.