【问题标题】:Using array from private access specifier使用私有访问说明符中的数组
【发布时间】:2021-06-19 11:20:38
【问题描述】:

这里我的类的私有部分中有一个数组,它无法保留在类的不同成员中设置的值;在战斗序列中,数组中的两个值都等于 0。

#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>
using namespace std;

class champions {
private:
    int health_array[2] = { };
    int attack_array[2] = { };
public:

void health_attack_set() {
    int health_set{};
    int attack_set{};
    for (int i = 0; i < 2; i++) {
        health_array[i] = health_set;
        attack_array[i] = attack_set;
    }
}
void fight_sequence(int random_champion, int random_opponent) {
    cout << "FIGHT\n\n";
    while (health_array[random_champion] > 0 or health_array[random_opponent] > 0) {
        (health_array[random_opponent] -= attack_array[random_champion]);
        if (health_array[random_opponent] <= 0) {
            break;
        }
        (health_array[random_champion] -= attack_array[random_opponent]);
    }
    if (health_array[random_champion] > 0) {
        cout << "CHAMPION 1 WINS!!!";
    }
    if (health_array[random_opponent] > 0) {
        cout << "CHAMPION 2 WINS!!!";
    }
    if (health_array[random_champion] == 0 && health_array[random_opponent] == 0) {
        cout << "NO ONE WINS!!";
    }
} 
void champion_1() {
    health_attack_set();
    health_array[0] = 400;
    attack_array[0] = 150;
}
void champion_2() {
    health_attack_set();
    health_array[1] = 500;
    attack_array[1] = 100;
}
};
int main() {
    champions fight;
    fight.fight_sequence(0, 1);
    return 0;
}

我相信这可能是一个简单的错误,但很难发现;感谢您提供的任何帮助。

【问题讨论】:

  • “无法保留不同对象中设置的值”中的“不同对象”在哪里?
  • 你在哪里打电话给champion_1()champion_2()
  • champion_1 和 Champion_2 是不同的对象。我在这些对象中设置的值不会在对象外部处理,例如:(health_array[0] = 400)。
  • 你需要阅读一本关于 C++ 的基础教科书。您的代码只有一个champions 类型的对象(在main() 中创建)。虽然您认为 champion_1()champion_2() 是对象,但没有人可以帮助您。
  • 这个结构就是麻烦。 champions 应该是class champion,就像std::vector&lt;champion&gt; 一样,其中每个英雄都有自己的属性。您在这里所做的几乎完全忽略了对象的意义。

标签: c++ arrays function class private


【解决方案1】:

我认为您的问题来自于不了解如何在存在类和多个对象的情况下构建代码。我以更简洁和更常见的方式重组了您的代码(这不是唯一的方法,但它更典型)。希望没有错别字。

#include <iostream>
#include <string>
#include<math.h>
#include<cstdlib>

using namespace std;

class Champion{
public:
    int health;
    int attack;

    Champion(int health_, int attack_) : health(health_), attack(attack_) {
    }
};

void fight_sequence(Champion& champion, Champion& opponent) {
    cout << "FIGHT\n\n";
    while (champion.health > 0 || opponent.health > 0) {
        (opponent.health -= champion.attack);
        if (opponent.health <= 0) {
            break;
        }
        (champion.health -= opponent.attack);
    }
    if (champion.health > 0) {
        cout << "CHAMPION 1 WINS!!!";
    }
    if (opponent.health > 0) {
        cout << "CHAMPION 2 WINS!!!";
    }
    if (champion.health == 0 && opponent.health == 0) {
        cout << "NO ONE WINS!!";
    }
}

int main() {
    Champion champion_1(400, 150);
    Champion champion_2(500, 100);

    fight_sequence(champion_1, champion_2);
    return 0;
}

【讨论】:

  • 感谢您的解决方案,但是否无法合并数组,因为我想在战斗中添加更多因素(例如:阻力)。一个数组(如果可能的话)可能有助于将每个冠军的统计数据放在同一个地方(就像一个班级的成员)。
  • @EvanBain 只需在Champion 类中添加一个成员变量,就像本例中显示的healthattack 一样?
  • 要做到这一点,您只需在类中添加一个新成员,例如int resistance; 下面的int attack;
  • 谢谢你,我是一个新的学习者,感谢我得到的所有帮助。
  • 注意:有些设计模式会为每个属性(例如健康、攻击和抵抗力)设置数组。但是,根据您发布的内容,它们超出了您的能力水平。我希望你不要认为这是一种冒犯——你需要从某个地方开始,而那些设计模式不是正确的地方。如果您对这些模式感到好奇并想了解这些模式,请在您对一般编程更加熟悉之后查找“数据驱动设计”和“实体组件系统”。
猜你喜欢
  • 2015-09-14
  • 2013-06-19
  • 2013-10-22
  • 2016-08-11
  • 1970-01-01
  • 2011-01-31
  • 1970-01-01
  • 2011-01-28
  • 2014-01-11
相关资源
最近更新 更多