繁体   English   中英

不完整的字段类型C ++

[英]Incomplete field type c++

我正在写一个小型的c ++游戏,但是我对c ++还是很陌生,因为通常我是用Java编写东西。 所以我不确定问题出在哪里。

页眉文件:

#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);

}

CPP-文件:

#include "Monster.h"
#include "Game.h"
#include "SpeedRatio.h"

//------------------------------------------------------------------------------
Monster::Monster()
{
}
//------------------------------------------------------------------------------
Monster::Monster(std::string name, unsigned int attack_damage,
                 unsigned int life, unsigned int attribute,
                 SpeedRatio speed_ratio, Game &game) : name_(name),
                 attack_damage_(attack_damage), life_(life),
                 attribute_(attribute), speed_ratio_(speed_ratio), // LINE 39
                 game_(&game)
{

}

SpeedRatio.h

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

现在我收到2条错误消息:

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

认为我的(希望)我的前瞻性声明是正确的。 我在行上加上了注释,以寻求帮助

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

前向声明( class SpeedRatio; )仅引入或声明类型的名称,但未定义类型,因此这是编译器认为类型不完整的原因。

解决方法可能是将相应的include从.cpp移至.h或改用引用或指针(smart_ptr或unique_ptr)。

您只声明了SpeedRatio类

class SpeedRatio;

但未定义。 因此,这种类型是不完整的:编译器不知道这种类型的对象的大小。 因此,编译器无法在Monster类的定义中定义数据成员speed_ratio_。

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; 

您应该在标头Monster.h包含标头SpeedRatio.h

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM