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"
usingnamespace 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"
usingnamespace 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;
};
$ 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)
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