-3

I am getting an error on my private variables Player_player; and Level _level; it is telling me that it is missing the type specifier and im not sure why this is. I have made classes for both Level and Player, so shouldnt I be able to use Player and Level to specify the variable type?

thanks for the help,

mike

//GameSystem.h

#pragma once
#include "Player.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>

using namespace std;

class GameSystem
 {
 public:
    GameSystem(string levelFileName);

    void playGame();

private:
    Level _level;
    Player _player;
  };
#

//Player.h

  #pragma once
  #include "GameSystem.h"
  #include "Level.h"
  #include <string>
  #include <iostream>
  #include <fstream>
  #include <cstdlib>
  #include <conio.h>
  #include <vector>

  using namespace std;

  class Player
  {
  public:
     Player();

void initPlayer(int level, int health, int attack, int defense, int experience);

    //Setters
void setPosition(int x, int y);

    //Getters
void getPosition(int &x, int &y);


private:

    //Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;

    //Position
int _x;
int _y;
  };

2 Answers2

2

Your GameSystem.h file has the line:

#include "Player.h"

Your Player.h file has the line:

#include "GameSystem.h"

This can't be good. And besides the Player class declaration in the header file doesn't use anything from GameSystem.h, so remove the GameSystem.h from the header file (I recommend removing all the #include that don't resolve anything in Player.h header file).

Edit 1:
I changed the header files and made them use Include Guards. I don't get any errors.
Player.hpp:

#ifndef PLAYER_HPP
#define PLAYER_HPP
  class Player
  {
  public:
     Player();

void initPlayer(int level, int health, int attack, int defense, int experience);

    //Setters
void setPosition(int x, int y);

    //Getters
void getPosition(int &x, int &y);


private:

    //Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;

    //Position
int _x;
int _y;
  };

#endif // PLAYER_HPP

GameSystem.hpp:

#ifndef GSYSTEM_HPP
#define GSYSTEM_HPP

#include "Player.hpp"
#include "Level.hpp"
#include <string>

using namespace std;

class GameSystem
 {
 public:
    GameSystem(string levelFileName);

    void playGame();

private:
    Level _level;
    Player _player;
  };

#endif // GSYSTEM_HPP

I changed the filename extensions to help distinguish between C language header files (.h) and C++ language header files (.hpp).

I also simplified the header files by remove unused include files.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

You have a dependency problem here, there are two solutions.

First, Player.h isn't actually dependent on GameSystem.h, so don't include GameSystem.h in Player.h.

//Player.h
#pragma once
//Remove GameSystem.h
//#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>

If, for some reason, you DO need the GameSystem class declaration in the Player class, just do this:

//Player.h
#pragma once
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>

//Declare the class
class GameSystem;

You will need to include the full header file in the .cpp file, but you don't actually need the full class definition to use a class in another classes definition (actually, GameSystem.h doesn't even need to include Player.h, it could just declare but not define the Player class)