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;
};
#include<iostream>
#include"Knight.h"
//#include"weapon.h"
usingnamespace 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)
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