C ++组合循环依赖

时间:2015-06-26 15:49:21

标签: c++ oop object-composition

我正在尝试学习C ++,目前我正试图了解如何用这种语言实现对象组合。

我有一个Character类,由HeroMonster类继承。

Character有一个NormalAbility和一个SpecialAbility

我已经制作了NormalAbilitySpecialAbility类,并且都继承了Ability超类。

我的问题是当我把#include" Character.h"在Ability.h中,Character.h中的normalAbility和specialAbility变量不会被识别为其受尊重的类。诸如"语法错误之类的错误:标识符字符串"显示在两个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
}

1 个答案:

答案 0 :(得分:2)

这样你就可以避免.h文件的循环包含,因为你将它包含在.cpp中,而你.h中你说的是,“字符存在但不关心他的定义是对的现在“

  

Ability.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;
}
  

Ability.cpp

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