C++ does not support default-int

C++ does not support default-int

本文关键字:default-int support not does C++      更新时间:2023-10-16

我的私有变量Player_Player出现错误;和Level _ Level;它告诉我它缺少类型说明符,我不确定为什么。我已经为Level和Player创建了类,所以我不应该使用Player和Level来指定变量类型吗?

谢谢你的帮助,

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

您的GameSystem.h文件具有以下行:

#include "Player.h"

您的Player.h文件具有以下行:

#include "GameSystem.h"

这不太好。此外,头文件中的Player类声明不使用GameSystem.h中的任何内容,因此从头文件中删除GameSystem.h(我建议删除Player.h头文件中没有解析任何内容的所有#include)。

编辑1:
我更改了头文件,并使它们使用Include Guards。我没有任何错误
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

我更改了文件扩展名,以帮助区分C语言头文件(.h)和C++语言头文件

我还通过删除未使用的包含文件简化了头文件。

这里有一个依赖性问题,有两种解决方案。

首先,Player.h实际上并不依赖于GameSystem.h,所以不要将GameSystem.h包含在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>

如果出于某种原因,您确实需要在Player类中声明GameSystem类,请执行以下操作:

//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;

您需要在.cpp文件中包含完整的头文件,但实际上并不需要完整的类定义来在另一个类定义中使用一个类(实际上,GameSystem.h甚至不需要包含Player.h,它可以只声明但不定义Player类)