对象变量未更新

时间:2019-06-18 17:40:23

标签: c++ oop

传递给dAttack方法的Player对象未在dAttack(player)上更新其hp。巨龙受到的伤害完全可以,但是不会对玩家造成伤害。我知道用C ++在您的主要函数中定义类可能不是最佳实践,但是当我尝试将它们放入自己的.cpp文件时,出现错误,指出“ Player”和“ Dragon”对象未初始化。

我试图将dAttack()中的hit变量重命名为dHit,但没有其他想法。

#include <iostream>
#include <string>


int main()
{
    class Player {
    public:
        float hMul;
        float dMul;
        float aMul;
        float hp;

        void setHMul(float h) { hMul = h; };
        void setDMul(float d) { dMul = d; };
        void setAMul(float a) { aMul = a; };
        void setHP() { hp = hMul * 100; };
    };

    class Dragon {
    public:
        float dHp;
        float dAt;

        void dAttack(Player player) {
            float dhit = dAt * 2;
            player.hp -= dhit;
            std::cout << "The dragon has hit you for: " << dhit << std::endl;
            std::cout << "You have " << player.hp << " hp left!" << std::endl;
        }

        void pAttack(Player player) {
            float hit = player.dMul * 2 + 3;
            dHp -= hit;
            std::cout << "You have hit the dragon for: " << hit << std::endl;
            std::cout << "The dragon has " << dHp << " hp left!" << std::endl;
        }
    };

    bool playing = true;
    std::cout << "Hello, please pick a character, (W)arrior, (Wi)zard or (R)ogue: ";

    std::string choice;
    std::cin >> choice;

    float hMul;
    float dMul;
    float aMul;

    if (choice == "W" || choice == "w" || choice == "Warrior" || choice == "warrior" || choice == "WARRIOR") {
        hMul = 10.0;
        dMul = 0.5;
        aMul = 3.0;
    } 
    else if (choice == "Wi" || choice == "wi" || choice == "WI" || choice == "Wizard" || choice == "wizard" || choice == "WIZARD") {
        hMul = 5.0;
        dMul = 5.0;
        aMul = 1.0;
    }
    else if (choice == "R" || choice == "r" || choice == "Rogue" || choice == "rogue" || choice == "ROGUE") {
        hMul = 3.0;
        dMul = 10.0;
        aMul = 1.5;
    }

    Player player;

    player.setHMul(hMul);
    player.setDMul(dMul);
    player.setAMul(aMul);
    player.setHP();

    Dragon dragon;

    dragon.dAt = 100.0;
    dragon.dHp = 10000.0;

    while (playing) {
        if (dragon.dHp > 0 && player.hp > 0) {
            dragon.pAttack(player);
            if (dragon.dHp > 0) {
                dragon.dAttack(player);
            }
            else {
                std::string ch;
                std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
                std::cin >> ch;
            }
        }
        else if (dragon.dHp <= 0) {
            std::string ch;
            std::cout << "The dragon has been defeated, would you like to play again? (y/n): ";
            std::cin >> ch;
        }
        else if (player.hp <= 0) {
            std::string ch;
            std::cout << "You have been defeated, would you like to play again? (y/n): ";
            std::cin >> ch;
        }
    }
}

示例输出:

You have hit the dragon for: 4
The dragon has 20 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 16 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 12 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 8 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 4 left!
The dragon has hit you for: 200
You have 800 hp left!
You have hit the dragon for: 4
The dragon has 0 left!
The dragon has been defeated, would you like to play again? (y/n):

1 个答案:

答案 0 :(得分:2)

一些一般性建议首先

  • 编写更少的代码,进行更多的测试。您面临的问题在代码中至少出现两次。如果您的代码中只有一个错误/错误,则错误/错误更容易修复。听起来有些疯狂,但是确保没有更多内容的简单方法是编写一行代码,并且只有在知道这样做的情况下,它才应该继续。
  • 当您遇到无法满足您期望的内容时,请编写一个很小的示例程序,仅包含该问题,仅此而已。首先,它可以帮助您找到/解决问题,如果找不到,您可以使用mcve进行发布。这将帮助我们为您提供帮助。

该问题的解决方案可能是这样的:

void take_dmg(int x) { x -= 1; }

int main() {
   int hp = 10;
   while (hp > 0) {
       take_dmg(hp);
   }
}

这将永远循环,并且hp中的main的值永远不会与10相同。这是因为函数中的xhp的副本。您想要的是

中的参考
void take_dmg(int& x) { x -= 1; }
              //^^ ------------------ pass by reference
int main() {
   int hp = 10;
   while (hp > 0) {
       take_dmg(hp);
   }
}

现在,x是对hp的引用。松散地说,引用只是原始值的别名,因此函数中的x递减会hp中的main递减。

您应该注意的术语是“按值传递”(第一个示例已损坏)和“按引用传递”(固定的)。

最后,不要在函数范围内声明类。在某些情况下,这是一种必要的语言功能,但一般而言,类应在函数外部(即也在main外部)声明。