简体   繁体   English

不完整的字段类型C ++

[英]Incomplete field type c++

I'm writing a small c++ game, but I'm quite new to c++ because normally I'm writing stuff in java. 我正在写一个小型的c ++游戏,但是我对c ++还是很陌生,因为通常我是用Java编写东西。 So I'm not sure where the problem is. 所以我不确定问题出在哪里。

Header-File: 页眉文件:

#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-File: 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 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);
}

Now I'm getting 2 error messages: 现在我收到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

T think my (hope) my forward declarations are right. 认为我的(希望)我的前瞻性声明是正确的。 I marked the lines with comments, thx for any help 我在行上加上了注释,以寻求帮助

You need to have complete definition of SpeedRatio class in your header file because you are using the complete object and not a reference or pointer. 您需要在头文件中具有SpeedRatio类的完整定义,因为您使用的是完整对象,而不是引用或指针。 The compiler needs to know the object size in order to generate the code. 编译器需要知道对象的大小才能生成代码。

Forward declaration( class SpeedRatio; ) only introduce a name of a type or declares it but does not define it so that's why the compiler says that the type is incomplete. 前向声明( class SpeedRatio; )仅引入或声明类型的名称,但未定义类型,因此这是编译器认为类型不完整的原因。

Fixes may be to move the corresponding include from .cpp to .h or to use a reference or pointer(smart_ptr or unique_ptr) instead. 解决方法可能是将相应的include从.cpp移至.h或改用引用或指针(smart_ptr或unique_ptr)。

You only declared class SpeedRatio 您只声明了SpeedRatio类

class SpeedRatio;

but not defined. 但未定义。 So this type is incomplete: the compiler does not know what will be the size of an object of this type. 因此,这种类型是不完整的:编译器不知道这种类型的对象的大小。 So the compiler is unable to define data member speed_ratio_ in the definition of class Monster. 因此,编译器无法在Monster类的定义中定义数据成员speed_ratio_。

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; 

You should include header SpeedRatio.h in header Monster.h 您应该在标头Monster.h包含标头SpeedRatio.h

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

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