c++ - 需要帮助让类(class)相互交流

标签 c++ class oop object

<分区>

所以基本上,我必须编写一个类似于角色扮演游戏的程序,其中有不同类型的生物。每个生物都是Creature类的一个对象,有生命值、力量等成员变量。

我遇到的问题是编写处理类之间处理和承受损害的函数。

#include <iostream>
#include <string>
#include <stdlib.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;

//Creature Base Class (Type 0)
class Creature
{
  public:
    Creature(int, int, int);
    int gettype() {return type;}
    int getstrength(){return strength;}
    int gethitpoints() {return hitpoints;}
    void sethitpoints(int);
    string getspecies();
    void printstats();
    void dealdamage(Creature, Creature);
    void takedamage(int);

  private:
    int type;
    int strength;
    int hitpoints;
};

Creature::Creature(int t, int s, int hp)
{
  type = t;
  strength = s;
  hitpoints = hp;
}

void Creature::printstats()
{
  cout << "This creature has: " << endl;
  cout << strength << " strength" << endl;
  cout << hitpoints << " hitpoints" << endl;
  cout << "and is of type " << type << "(" << getspecies() << ")" << endl;
}

void Creature::sethitpoints(int a)
{
  hitpoints = a;
}

string Creature::getspecies()
{
  switch(type)
  {
    case 0: return "Creature";
    case 1: return "Human";
    case 2: return "Elf";
    case 3: return "Demon";
    case 4: return "Balrog";
    case 5: return "Cyberdemon";
  }
}

void Creature::dealdamage(Creature dealer, Creature target)
{
  srand(5);
  int damage;
  damage = rand() % strength+1;
  cout << dealer.getspecies() << " inflicts " << damage;
  cout << " damage to " << target.getspecies() << "!" << endl;
  target.takedamage(damage);
}

void Creature::takedamage(int damage)
{
  sethitpoints((gethitpoints()-damage));
}

int main()
{
  Creature monster1(0, 10, 100);
  Creature monster2(1, 7, 90);

  monster1.printstats();
  monster2.printstats();

  monster1.dealdamage(monster1, monster2);
  monster2.printstats();

  return 0;
}

现在,程序给我的输出是:

This creature has:
10 strength
100 hitpoints
and is of type 0(Creature)
This creature has:
7 strength
90 hitpoints
and is of type 1(Human)
Creature inflicts 5 damage to human!
This creature has:
7 strength
90 hitpoints
and is of type 1(Human)

所以 dealdamage() 函数似乎起作用了,但是 takedamage() 函数没有正确地改变受到伤害的生物的生命值。

如有任何帮助,我们将不胜感激。

最佳答案

问题是 void Creature::dealdamage(Creature dealer, Creature target)

首先,这称为“按值传递”。构造新的“Creature”对象,并将调用函数时使用的“Creature”对象的值复制到其中。该过程执行,并且这些临时 Creature 对象 EOL - 原始 Creature 对象永远不会被触及。

您需要获取原始对象的指针或引用。但是除非你打算支持某种三向战斗,否则你不应该同时要求这两个项目——这是一个非静态成员函数,所以它已经在其中一个生物的上下文中运行,因此您调用它的语法: monster1.dealdamage(monster1, monster2);

像这样改变你的交易伤害:

void Creature::dealdamage(Creature& target) // takes a reference
{
    //srand(5); <- this will cause rand() to always return the same value. dont do it.
    //int damage; <- don't separate declaration and assignment when you can avoid it.
    int damage = rand() % strength+1;
    cout << getspecies() << " inflicts " << damage
         << " damage to " << target.getspecies() << "!" << endl;
    target.takedamage(damage);
}

如果您发现仅使用 getspecies() 不清楚,您可以使用 this->getspecies()

尝试像“srand(time(NULL))”之类的东西代替 srand(常量值),或者更好的是,在程序开始时执行一次。

关于c++ - 需要帮助让类(class)相互交流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879747/

相关文章:

c++ - 从url下载图片并放入缓冲区

c++ - STL 容器、SBO 和自定义分配器冲突

带有类的 Javascript/jQuery 回调

oop - 逻辑设计与物理设计

python - Python中的装饰器@staticmethod是如何去掉实例对象的?

C++ boost共享数组交换指针(简单问题)

c++ - 使用动态参数创建线程

c# - 如何解决 'Invalid initializer member declarator' 错误 C#,从 CSV 文件读取

python - 是否可以为python中的类设置默认方法?

javascript - 创建嵌套 ES6 类的高效而优雅的方法?