0

I can't understand why I am getting this errors. When I make the constructor it shows this errors 'bag' is not a type or 'bag' does not name a type and if i delete the constructor, it compiles.

#include <iostream>
#include "bag.h"

using namespace std;

#ifndef PLAYER_H
#define PLAYER_H

class player {
        string name;
        bag *bag;
    public:
        player(string, bag *);
};

player::player(string name = "DEFAULT_NAME", bag *bag = NULL)
{
    this->name = name;
    this->bag = bag;
}

#endif /* PLAYER_H */

Bag class works fine and compiles, but as soon as I try to make a constructor with bag class I get this errors.

Padelis
  • 41
  • 6
  • The guards should be 1st, not 3rd. The `using namespace std;` should not be anywhere in your code at all. Cannot reproduce, because this isn't a [mcve]. – Eljay Jun 15 '20 at 20:28
  • 2
    You need to decide: is `bag` the name of a *class* or the name of a member variable (and function parameter)? It cannot be both. – Adrian Mole Jun 15 '20 at 20:33
  • does `bag.h` include this header? – M.M Jun 15 '20 at 20:37
  • 1
    You should not have `player` constructor body in the header (or if you do, mark it as `inline`) – M.M Jun 15 '20 at 20:37
  • Okay I did change the guards and removed using namespace std; (it was by mistake) Also I changed the name but i don't know why I didn't had any problems with other classes like ```item item;```. But thank you it works fine know. – Padelis Jun 15 '20 at 20:38
  • Make sure that `bag.h` does not include `player.h` not even via a third header or any combination of headers. – drescherjm Jun 15 '20 at 21:13
  • fwiw, `item item;` seems to be fine (i am a bit surprised) but not as member: https://godbolt.org/z/yzwQGG, in any case it is poor naming – 463035818_is_not_an_ai Jun 15 '20 at 21:28
  • In ```main.cpp``` i have declared ```item item``` and it works fine. Also thanks about the link is usefull – Padelis Jun 15 '20 at 21:44

0 Answers0