不完整的字段类型c++; 我写了一个小型C++游戏,但我对C++很陌生,因为我通常用java编写东西。所以我不确定问题出在哪里

不完整的字段类型c++; 我写了一个小型C++游戏,但我对C++很陌生,因为我通常用java编写东西。所以我不确定问题出在哪里,c++,header-files,C++,Header Files,头文件: #include <string> class Game; class SpeedRatio; class Monster { private: ... SpeedRatio speed_ratio_; // LINE 36 ... public: Monster(); //--------------------------------------------------------------------------

头文件:

#include <string>
class Game;
class SpeedRatio;

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; // LINE 36
    ...
public:

    Monster();

    //--------------------------------------------------------------------------
    // Init Constructor
    // @param name the monsters name.
    // @param attack_damage the monsters attack damage.
    // @param life the monsters life.
    // @param attribute the monsters attribute.
    // @param speed_ratio the speed ratio.
    // @param game the game object.
    //
    Monster(std::string name, unsigned int attack_damage, unsigned int life,
            unsigned int attribute, SpeedRatio speed_ratio, Game &game);

}
速比

class SpeedRatio
{
  private:
    unsigned int events_;
    unsigned int ticks_;

  public:

    SpeedRatio();

    //--------------------------------------------------------------------------
    // Init Constructor
    // @param events the events.
    // @param ticks the ticks.
    SpeedRatio(unsigned int events, unsigned int ticks);
}
现在我收到两条错误消息:

Description Resource Path Location Type field 'speed_ratio_' has incomplete type Monster.h /ass1 line 36 C/C++ Problem
Description Resource Path Location Type class 'Monster' does not have any field named 'speed_ratio_' Monster.cpp /ass1 line 39 C/C++ Problem
说明资源路径位置类型 字段“速度比”的怪物类型不完整。h/ass1第36行C/C++问题
说明资源路径位置类型 类“Monster”没有任何名为“speed\u ratio\u”Monster.cpp/ass1第39行的字段C/C++问题
I don’我不认为我的(希望)声明是正确的。
我用注释标记了行,thx要获得任何帮助

您需要在头文件中有
速度比
类的完整定义,因为您使用的是完整对象,而不是引用或指针。编译器需要知道对象大小才能生成代码

正向声明(
class SpeedRatio;
)只引入或声明类型的名称,但不定义它,因此编译器会说类型不完整


修复方法可能是将相应的include从.cpp移动到.h,或者改用引用或指针(smart\u ptr或unique\u ptr)。

您需要在头文件中完整定义
速度比
类,因为您使用的是完整的对象,而不是引用或指针。编译器需要知道对象大小才能生成代码

正向声明(
class SpeedRatio;
)只引入或声明类型的名称,但不定义它,因此编译器会说类型不完整


修复方法可能是将相应的include从.cpp移动到.h,或者使用引用或指针(smart\u ptr或unique\u ptr)。

您只声明了类速度比

class SpeedRatio;
但没有定义。因此,这种类型是不完整的:编译器不知道这种类型的对象的大小。因此,编译器无法在类Monster的定义中定义数据成员速度比

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; 

您应该在标题
Monster.h
中包含标题
SpeedRatio.h

您只声明了类速比

class SpeedRatio;
但没有定义。因此,这种类型是不完整的:编译器不知道这种类型的对象的大小。因此,编译器无法在类Monster的定义中定义数据成员速度比

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; 

您应该在标题
SpeedRatio.h
中包含标题
Monster.h

基于标题的搜索的可能重复项有很多相关结果基于标题的搜索的可能重复项有很多相关结果