[英]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.