簡體   English   中英

C ++組合循環依賴

[英]C++ composition circular dependency

我正在嘗試學習C ++,目前正在嘗試了解如何以這種語言實現對象組合。

我有一個Hero類和Monster類繼承的Character類。

Character具有NormalAbilitySpecialAbility

我已經創建了NormalAbilitySpecialAbility類,並且都繼承了Ability超類。

我的問題是,當我把#包括“Character.h”在Ability.h在normalAbility和specialAbility變量Character.h沒有得到承認他們的尊重類。 兩種Ability繼承類的標頭中均顯示諸如“語法錯誤:標識符字符串”之類的錯誤

這是我的代碼:

Character.h

#pragma once
#include <string>
#include "NormalAbility.h"
#include "SpecialAbility.h"

using namespace std;

class Character
{
public:
   Character(string name, string type, int hp, NormalAbility na, 
             SpecialAbility sa);
   bool isDead();
   void damage(int amt);
   void heal(int amt);
   void attack(Character* c, int amt);

private:
   string name;
   string type;
   int hp;
   int maxHp;
   NormalAbility* normalAblity;
   SpecialAbility* specialAbility;
}

Character.cpp

#include "Character.h"
#include <iostream>

Character::Character(string name, string type, int hp, NormalAbility* na, 
             SpecialAbility* sa)
{
   this->name = name;
   this->type = type;
   this->maxHp = hp;
   this->hp = hp;
   normalAbility = na;
   specialAbility = sa;
}

bool Character::isDead(){
   return hp <= 0;
}

void Character::damage(int amt){
   if (hp > 0){
      hp -= amt;
   }
   else{
      hp = 0;
   }
}

void Character::heal(int amt){
   if (hp + amt > maxHp){
      hp = maxHp;
   }
   else{
      hp += amt;
   }
}

void Character::attack(Character* c, int amt){
   c->damage(amt);
}

Hero.h

#pragma once
#include "Character.h"
#include <string>

using namespace std;

class Hero :
   public Character
{
public:
   Hero(string name, int hp);
}

Hero.cpp

#include "Hero.h"
#include <iostream>

Hero::Hero(string name, int hp) 
     : Character(name, "Hero", hp)
{
}

Ability.h

#pragma once
#include <string>
#include "Character.h"

using namespace std;

class Ability
{
public:
   Ability(string name, string type, Character* owner);
   void levelUp();
private:
   string name;
   string type;
   int level;
   Character* owner;
}

Ability.cpp

#include "Ability.h"

Ability::Ability(string name, string type, Character* owner)
{
   this->name = name;
   this->type = type;
   this->owner = owner;
   level = 1;
}

void Ability::levelUp(){
   level++;
}

NormalAbility.h

#pragma once
#include "Ability.h"
#include <string>

using namespace std;

class NormalAbility :
   public Ability
{
public:
   NormalAbility(string name);
}

NormalAbility.cpp

#pragma once
#include "NormalAbility.h"
#include <string>

using namespace std;

NormalAbility::NormalAbility(string name) : Ability(name, "Normal")
{
  //some codes
}

這樣,您就避免了.h文件的循環包含,因為您將其包含在.cpp中,而在.h中則表示,“字符存在,但現在不在乎他的定義”

能力

#pragma once
#include <string>


class Character;

using namespace std;

class Ability
{
public:
   Ability(string name, string type, Character* owner);
   void levelUp();
private:
   string name;
   string type;
   int level;
   Character* owner;
}

能力

#include "Ability.h"
#include "Character.h"

Ability::Ability(string name, string type, Character* owner)
{
   this->name = name;
   this->type = type;
   this->owner = owner;
   level = 1;
}

void Ability::levelUp(){
   level++;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM