How to use a struc in a class - c++

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.

Related

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++: Want to make an easy nested struct

I'm a C++ beginner and trying to make a nested struct which has 2 sub-structs under it.
The code is:
struct Sub_number{
int one;
int two;
};
struct Sub_size{
int width;
int height;
};
struct MainStruct{
struct Sub_number number;
struct Sub_size size;
}main;
and I got [Cannot use dot operator on a type] error from Xcode when I tried to put a value in it like this:
main.number.one = 13;
^
Does anyone has any ideas what's wrong with this code...?
Thank you so so much everyone. As you wrote, the name I've using was the no-good point!! Silly me.. I'll double check when I'm going to ask on StackOverflow next time.
Thanks!
main is the reserved word for main function (starting point of application) you need to change the variable name to something else . This will fix the issue
struct Sub_number {
int one;
int two;
};
struct Sub_size {
int width;
int height;
};
struct MainStruct {
struct Sub_number number;
struct Sub_size size;
}someVariable;
void main() {
someVariable.number.one = 1;
}
your struct name can't be main .main is a unique function name int main().change the struct name to others!

Declaring a Class C++

I am struggling knowing how to create a class. I want to create a "Player" class and all I want to do is pass in the name while I'll have the other variables start at 0 until they are updated when a game is run (later in the program)
Player::Player(string name_in)
{
name = name_in;
int numOfWins = 0;
int numOfLoses = 0;
int numOfDraws = 0;
int totalMatches = 0;
}
Right now there are lots of errors around numOfWins, numOfLoses, numOfDraws and totalMatches. What can I do to fix this?
Perhaps the error is in your int ... part of assignments, which essentially creates a new local variable in a constructor.
Try this version:
#include <string>
using namespace std;
class Player
{
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in)
{
name = name_in;
numOfWins = 0;
numOfLoses = 0;
numOfDraws = 0;
totalMatches = 0;
}
};
You should declare other instance variables in the class declaration, rather than declaring them as locals (which is completely useless).
// This part goes in the header
class Player {
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in);
};
Now in the constructor you could use initialization lists:
// This part goes into the CPP file
Player::Player(string name_in)
// Initialization list precedes the body of the constructor
: name(name_in), numOfWins(0), numOfLoses(0), numOfDraws(0), totalMatches(0) {
// In this case, the body of the constructor is empty;
// there are no local variable declarations here.
}
Kinda vague, but I'll take a crack at it. You Probably want:
class Player{
string name;
int numOfWins;
int numOfLosses;
int numOfDraws;
int totalMatches;
Player(string name_in)
};
Player::Player(string name_in){
name = name_in;
numOfWins = 0;
numOfLosses = 0;
numOfDraws = 0;
totalMatches = 0;
}
Haven't used C++ in a while, so this may be faulty.
The errors you get, at least from the snippet you posted are caused for you can't declare variables in constructor - you declare them in class body and initialize in constructor or using another function.
#include <string>
class Player {
public:
Player( std::string const& name_in) : name( name_in),
numOfWins(), numOfLoses(),
numOfDraws(), totalMatches()
{} // constructor
// will initialize variables
// numOfWins() means default
// initialization of an integer
private:
std::string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
};
usage:
int main() {
Player( "player_one");
return 0;
}

cant get subclasses to work properly

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.
};

C++ SDL_Rect inside a struct: Does not name a type

I am making a basic game and to make things easier I have used:
struct entity {
int health;
int damage;
SDL_Rect hitbox;
} player, basicEnemy[10];
But when I call:
player.hitbox.x = 5;
or something similiar, I get the error:
'player' does not name a type
How do I fix this?
Add typedef in front of the key word struct.