cant get subclasses to work properly - c++

I making a simple text based fighting game and i am having a lot of trouble getting my subclasses to work.
of the many errors im getting, the most persistant is "our of line definition "Dwarf" does not match any declaration of "Dwarf"
#include <iostream>
using namespace std;
class Poke{
protected:
string race;
int health, damage, shield;
public:
Poke();
Poke(int health, int damage, int shield);
virtual int attack(Poke*);
virtual int defend(Poke*);
virtual int getHealth();
};
this is one sublcasses of the different races, there are 2 more with different levels of attack/health/shield
// Dwarf: High health, Low attack, High defense
class Dwarf: public Poke {
public:
string race = "Dwarf";
int attack(Poke*);
int defend(Poke*);
int getHealth();
};
.cpp V
//DWARF
Dwarf::Dwarf(int health, int damage, int shield) {
this->health = 100;
this->damage = 50;
this->shield = 75;
};
//attack
int Poke:: attack(Poke*){
if (shield > (damage + rand() % 75)){
cout << "Direct Hit! you did" << (health - damage) << "points of damage";
}
else {std::cout << "MISS!"<<;
}
return 0;
};
int Poke:: attack(Poke*){
Enemy this->damage ;
};
i am using a player class for the person playing the game that will use "Poke"
class Player{
int wins, defeats, currentHealth;
string name;
Poke race;
bool subscribed;
public:
Player(int wins, int defeats, int currentHealth);
int addWins();
int addDefeats();
int getWins();
int getDefeats();
int getHealth();
};
.cpp V
//getHealth
int Player::getHealth(){
return this->currentHealth;
};
and and "enemy" class for the computer opponent:
class Enemy{
int eHealth;
Poke eRace;
public:
Enemy (int eHealth, Poke eRace);
int getEHealth;
};
.cpp V
int Enemy:: getEHealth(){
return this->eHealth;
};
any help would be much appreciated!!

Constructors are not inherited. You will have to declare a Dwarf constructor that matches your definition.
I think you'll also have trouble with this:
string race = "Dwarf";
You can't initialize class members that way. It will have to be initialized in a constructor.
Edit:
You don't seem to understand what I mean by a declaration. Change your Dwarf class declaration to look something like this:
// Dwarf: High health, Low attack, High defense
class Dwarf: public Poke {
public:
string race;
Dwarf(int health, int damage, int shield); // <-- constructor declaration
int attack(Poke*);
int defend(Poke*);
int getHealth();
};
Edit 2:
Your Dwarf constructor should also call the Poke constructor, like so:
Dwarf::Dwarf(int health, int damage, int shield) :
Poke(health, damage, shield),
race("Dwarf")
{
// Nothing needed here.
};

Related

How can I best decouple two classes, which one way dependencies

I am attempting to create a D&D combat encounter simulator using C++ and since it's D&D, all aspects of the simulation are going to depend heavily on the "Dice" Class and its methods.
I could instantiate a "Dice" object every time another class needs
to invoke its methods, however, that would leave everything heavily
coupled and make it very difficult to make changes or extensions later.
I don't have any practical knowledge about things such as Factories, dependency injections, and other such methods.
My question therefore in essence is:
What would be the best way to ensure that the "Dice" class remains as
decoupled as possible from all other classes?
While still enabling them to make use of the "Dice" objects, and its methods, when needed.
Dice.h
#ifndef dice_h_
#define dice_h_
#include <stdlib.h>
class Dice
{
private:
int maxValue;
public:
Dice(int maxValue);
~Dice();
int getMaxValue( void ){return maxValue;}
void setMaxValue(int newMaxValue){maxValue = newMaxValue;}
int rollDice();
int rollMultipleDice(int numberOfDiceRolls);
};
#endif
Dice.cpp
#ifndef dice_cpp_
#define dice_cpp_
#include "dice.h"
Dice::Dice(int maxValue){this->maxValue = maxValue;}
Dice::~Dice(){}
int Dice::rollDice()
{
return (rand() % maxValue) + 1;
}
int Dice::rollMultipleDice(int numberOfDiceRolls)
{
int i = numberOfDiceRolls, sum = 0;
while(i-- > 0)
{
sum += rollDice();
}
return sum;
}
#endif
Actor.h
#ifndef actor_h_
#define actor_h_
#include "dice.h"
class Actor
{
private:
unsigned int hp;
unsigned int ac; // Armor Class
unsigned int dmg;
public:
Actor(unsigned int hp, unsigned int ac, unsigned int dmg);
~Actor();
unsigned int getHP( void );
unsigned int getAC( void );
unsigned int getDmg( void );
void setHP( unsigned int newHP);
void setAC( unsigned int newAC);
void setDmg( unsigned int newDmg);
void attack(Actor* target);
bool isHit(Actor target);
};
#endif
Actor.cpp
#ifndef actor_cpp_
#define actor_cpp_
#include "actor.h"
Actor::Actor(unsigned int hp, unsigned int ac, unsigned int dmg)
{
this->hp = hp;
this->ac = ac;
this->dmg = dmg;
}
Actor::~Actor(){}
unsigned int Actor::getHP( void ){return hp;}
unsigned int Actor::getAC( void ){return ac;}
unsigned int Actor::getDmg( void ){return dmg;}
void Actor::setHP( unsigned int newHP ){this->hp = newHP;}
void Actor::setAC( unsigned int newAC ){this->ac = newAC;}
void Actor::setDmg( unsigned int newDmg ){this->dmg = newDmg;}
void Actor::attack(Actor* target)
{
Dice damageDice(8);
if (isHit(*target))
{
target->setHP(target->getHP() - damageDice.rollDice());
}
}
// helper function to attack function
// do not use elsewhere
bool Actor::isHit(Actor target)
{
Dice atkDice(20);
return atkDice.rollDice() >= target.getAC();
}
#endif
You talk about singletons and the like in the question so do you want ever class that needs to use the dice to use the same instance of the Dice class?
If so since the Dice class doesn't have any states to hold onto it can be a static class. I would then remove the MaxValue and add an input to the RollDice and RollMutiDice to take a Max Value (which I am guessing is suppose to the "sides" of the dice). So a call to Dice::RollDice(3) is to roll a 3 sided dice or Dice::RollMutiDice(6,3) would roll a 6 sided dice 3 times.
Also make sure you send the rand() class. I think is it srand(int). Usually you pass it time so it is fairly random

Stl list of struct as a class member

For my homework I have to do a class Game in C++ that has name, size, and a list of updates that contain date of update and some information about that update. (for example : 22.05.2018 Bug fixed at quest 3). Here is what i tried, but it doesn't work. Game.h:
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game();
};
and in Game.cpp:
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
In int main I created a list:
int main()
{
list<update>mylist;
update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
i get this error:
no matching function for call to 'Game::Game(const char [4], double, std::__cxx11::list<update>&)'|
Or if you want to keep the nested class update:
#include <string>
#include <list>
using std::string;
using std::list;
class Game{
public:
struct update{
string date;
string info;
};
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
virtual ~Game() {}
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main()
{
list<Game::update> mylist; // use Game::update to access nested class
Game::update u1,u2,u3;
u1.date="20.05.2018";
u1.info="Mission 3 bug fixed";
u2.date="25.05.2018";
u2.info="New quest";
mylist.push_back(u1);
mylist.push_back(u2);
Game g("Gta5",60.0,mylist);
return 0;
}
Try this,
#include<bits/stdc++.h>
using namespace std;
struct update
{
string date;
string info;
update(string date,string info){
this->date=date;
this->info=info;
}
};
class Game{
public:
string name;
double size;
list<update>l;
Game(string name, double size, list<update>l);
// virtual ~Game();
};
Game::Game(string name, double size, list<update>l){
this->name=name;
this->size=size;
this->l=l;
}
int main(){
list<update> l,m;
l.push_front(update("10/10/2017","some bug fixed"));
double size=100;
string name="Game1";
Game obj(name,size,l);
cout<<obj.name<<" "<<obj.size<<" "<<endl;
m=obj.l;
list<update>::iterator i;
for(i=m.begin();i!=m.end();i++){
update structObj=*i;
cout<<structObj.date<<" "<<structObj.info<<endl;
}
return 0;
}
Output
Game1 100
10/10/2017 some bug fixed

How to use a struc in a class

so ive been doing c++ for a little while now but im wondering how to use a struct in a class, lets say i was making a FPS game i created a basic structure for a gun:
struct gun
{
int damage;
string name;
int number_of_bullets;
};
and i created a class for a enemy like this:
class enemy
{
const int max_health = 100;
int health;
int damage;
gun mgun;
};
when i compile the program i get a error that sais: 'gun' does not name type.
what am i doing wrong? thanks.
1) you have to define "gun" before your class.
2) put a semicolon at end of struct "gun"
struct gun
{
int damage;
string name;
int number_of_bullets;
};
3)in your class , the "const int max health = ...." is wrong put an '_' between "max" & "health" or something else.

C++ RPG Error: data member initializer is not allowed

this has already been posted several times, but none of the times have answered my case. Please help me with my 'error: data member initializer is not allowed' which appears under the equals signs. Here's the code with the problem in it.
//Player.cpp :Contains information about the player
#include <iostream>
#include <string>
#include "Main.cpp"
using namespace std;
void Player()
{
struct Player {
int Charma = 0;
unsigned int Hunger = 10;
unsigned int Energy = 50;
unsigned int Health = 100;
};
enum Race {
UNKNOWN,
DEAD,
HUMAN,
ORC,
GOBLIN,
ELF,
LIZARD,
CAT,
VAMPIRE,
WEREWOLF,
SNK
};
}
You are getting that error because you are initializing the variables when you are declaring a struct. This is not allowed. Instead, move the initialization into the constructor of the struct.
However that is not the only error in your code. You are defining the struct inside of the Player function (which should be the constructor). You need to switch those, so that you have the Player function inside of the Player struct. This way the struct will have a constructor where you can initialize the values. Another thing, don't #include .cpp files. It's a bad practice.
Your code should be something like this:
struct Player {
int Charma;
unsigned int Hunger;
unsigned int Energy;
unsigned int Health;
Player() : Charma(0), Hunger(10), Energy(50), Health(100)
{
// do other constructor stuff here
}
};
In another approach of idea, if you are planning on doing some mecanics inside Player, you may move the declaration inside a real class. You will then be able to scale your project a little more easily. Something like this:
Header
// #include "Item.h"
typedef enum RaceDef {
UNKNOWN,
DEAD,
HUMAN,
ORC,
GOBLIN,
ELF,
LIZARD,
CAT,
VAMPIRE,
WEREWOLF,
SNK
} PlayerRace;
class Player {
public:
Player(unsigned int Charma=0,
unsigned int Hunger=10,
unsigned int Energy=50,
unsigned int Health=100,
PlayerRace Race=HUMAN);
void attack(Player);
void slap(Player);
//void equipItem(Item);
void exercise(unsigned int duration);
void die();
void etc();
private:
unsigned int m_Charma;
unsigned int m_Hunger;
unsigned int m_Energy;
unsigned int m_Health;
unsigned PlayerRace m_Race;
Race m_Race;
};
CPP
Player::Player(unsigned int Charma,
unsigned int Hunger,
unsigned int Energy,
unsigned int Health,
PlayerRace Race):
m_Charma(Charma),
m_Hunger(Hunger),
m_Energy(Energy),
m_Health(Health),
m_Race(Race) {
//constructor code goes here
//e.g. if player starts with a random item :
// Item randomItem = ItemUtils.getRandomItem();
// equipItem(randomItem);
}
I hope this helps, and good luck :)

c++: public private error

so i've got this problem.
i want to get some variables from a diferant class then main and i've lerned that it's good to
hide your data so it wont get so easely changed and use a getXXX function to acces it.
I tried to use the private: and public: thing but when i do i get an error saying
error: expected unqualified-id befor 'private'
i got class nr1# called dialog and the class with the variables is called race (not as in black and white)
anyway i call the function like this:(class dialog)
above this is all the #include stuff
dialog::dialog(int y)
{
race raceO;
switch(y)
{
case 1: cout << "choose a class \n1 ELF = " << raceO.getStats(1.1) << endl;
break:
}
and this is the race class
//were am i supposed to put
private: and public:
include "race.h"
include <iostream>
include <string>
using namespace std;
race::race(){
}
int race::raceElf(){
return 0;
}
int attack = 5;
int defence = 3;
int stamina = 6;
int race::getStats(int x){
if(x == 11){
return attack;
}
return 0;
}
in the Class declaration it should look like
class myClass{
public:
myClass();
private:
double x,y,z;
}
that is how you should use public and private, but otherwise I can't see what is wrong, plase provide the header file or the class declaration.
It depends how you want to use your class. I would keep all your data members private. Methods raceElf() and getStats() should be public as you will call them on your object. If a method were used only within other class methods but not outside it should be private. Constructor must be public if you want to create any objects of your class.
class race{
public:
race();
int raceElf();
int getStats(int);
private:
int attack;
int defence;
int stamina;
}
race::race(){
attack = 5;
defence = 3;
stamina = 6;
}
int race::raceElf(){ return 0; }
int race::getStats(int x){
if(x == 11){
return attack;
}
return 0;
}