【问题标题】:C++ does not support default-int [closed]C++ 不支持默认整数 [关闭]
【发布时间】:2015-06-13 18:23:35
【问题描述】:

我的私有变量 Player_player 出现错误;和级别_level;它告诉我它缺少类型说明符,我不确定这是为什么。我已经为Level和Player都做了类,所以我不应该能够使用Player和Level来指定变量类型吗?

感谢您的帮助,

麦克

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

//播放器.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;
  };

【问题讨论】:

  • 能否请您附上完整的错误信息?
  • 心理调试器说:递归标头。
  • 错误 C2146:语法错误:缺少 ';'在标识符“_player”之前出现错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
  • 请编辑问题以包含错误消息。这意味着 Player 那时没有定义。没有看到 Player.h,我无法说出原因。
  • 为什么 Player.h 需要包含 GameSystem.h?

标签: c++ class variables types ascii


【解决方案1】:

您的GameSystem.h 文件包含以下行:

#include "Player.h"

您的Player.h 文件包含以下行:

#include "GameSystem.h"

这可不好。除了头文件中的Player 类声明不使用GameSystem.h 中的任何内容,因此请从头文件中删除GameSystem.h(我建议删除所有在Player.xml 中无法解析任何内容的#include。 h头文件)。

编辑 1:
我更改了头文件并让它们使用Include Guards。我没有收到任何错误。
播放器.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++ 语言头文件 (.hpp)。

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

【讨论】:

  • 另外,从头文件中删除using namespace std;。您的 Player 类不使用来自 namespace std 的任何内容。
【解决方案2】:

你这里有依赖问题,有两种解决方案。

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

【讨论】:

  • 还有一个第三种解决方案,那就是从Player.h中删除#include "GameSystem.h",因为Player类没有引用GameSystem
  • @Thomas Matthews,这就是我的第一个解决方案
  • 提示依赖问题有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
相关资源
最近更新 更多