简体   繁体   English

C ++组合循环依赖

[英]C++ composition circular dependency

I'm trying to learn C++ and currently I'm trying to know how to implement an object composition in this language. 我正在尝试学习C ++,目前正在尝试了解如何以这种语言实现对象组合。

I have a Character class which is inherited by a Hero and a Monster class. 我有一个Hero类和Monster类继承的Character类。

A Character has a NormalAbility and a SpecialAbility . Character具有NormalAbilitySpecialAbility

I've made the NormalAbility and SpecialAbility classes and both are inheriting an Ability superclass. 我已经创建了NormalAbilitySpecialAbility类,并且都继承了Ability超类。

My problem is that when I put the #include "Character.h" in Ability.h the normalAbility and specialAbility variables in Character.h don't get recognized as their respected classes. 我的问题是,当我把#包括“Character.h”在Ability.h在normalAbility和specialAbility变量Character.h没有得到承认他们的尊重类。 Errors such as "syntax error : identifier string" shows in the headers of both Ability inherited classes 两种Ability继承类的标头中均显示诸如“语法错误:标识符字符串”之类的错误

Here's my code: 这是我的代码:

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
}

This way you avoid the circular include of the .h files, because you're including it in the .cpp, and in the .h you're saying that, "Character exists but don't care about his definition right now" 这样,您就避免了.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++;
}

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

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