需要帮助让类彼此交互

时间:2013-06-02 04:22:51

标签: c++ class oop object

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

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

#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()函数没有正确地改变受到伤害的生物的生命值。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是    void Creature :: dealdamage(生化经销商,生物目标)

首先,这称为“按值传递”。构造新的“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(常量值)尝试类似'srand(time(NULL))',或者更好的是,在程序开始时执行一次。