Pergunta

I'm writing a small c++ game, but I'm quite new to c++ because normally I'm writing stuff in 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:

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

Now I'm getting 2 error messages:

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

Foi útil?

Solução

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. 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.

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.

Outras dicas

You only declared class 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.

class Monster
{
  private:
    ...
    SpeedRatio speed_ratio_; 

You should include header SpeedRatio.h in header Monster.h

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top