鱼生富贵 2022-12-23 22:08 采纳率: 97.4%
浏览 47
已结题

关于#c++#的问题:巫师的attrack和cure可以帮助player升级power和lasting,为什么我在主函数里调用巫师技能最后输出显示技能发动前后玩家的power和lasting值不变呢


#include<iostream>
using namespace std;

enum FAMILY { SUN, MOON, HUMAN };

class Weapon {
private:
    string name;
    int degree;
    float power;
    float lasting;
public:
    Weapon() { power = 0; lasting = 0; degree = 0; }
    Weapon(string _name, float _power = 0, float _lasting = 0, int _degree = 0)
    {
        name = _name;  power = _power; lasting = _lasting; degree = _degree;
    }
    string GetName() { return name; }
    int GetDegree() { return degree; }
    float GetPower() { return power; }
    float GetLasting() { return lasting; }
    void upgrade() { degree++; }
    void uppower(float push) { power += push; }
    void uplasting(float push) { lasting += push; }
    void show()
    {
        cout << "&weapon information:" << endl;
        cout << "name: " << name << endl;
        cout << "degree: " << degree << endl;
        cout << "power: " << power << endl;
        cout << "lasting: " << lasting << endl;
    }
};

class Player {
private:
    string name;
    int experience;
    FAMILY family;
    Weapon weapon;
    static int number;
public:
    friend class Wizard;
    Player() { experience = 0; }
    Player(string _name, Weapon _weapon, int _experience = 0) :weapon(_weapon)
    {
        name = _name; experience = _experience;
        number++;
        family = FAMILY(rand() % 3);
    }
    string getName() { return name; }
    int getExperience() { return experience; }
    FAMILY getFamily() { return family; }
    Weapon getWeapon() { return weapon; }
    Player(const Player& pla)
    {
        name = pla.name;
        experience = pla.experience;
        family = pla.family;
        number++;
    }
    ~Player() { number--; }
    Player& operator=(const Player& pla)
    {
        name = pla.name;
        experience = pla.experience;
        family = pla.family;
        return *this;
    }
    void upgrade(int push)
    {
        experience += push;
        if (push > 50)
            weapon.upgrade();
    }
    void show()
    {
        cout << "=========================================" << endl;
        cout << "**player information:" << endl;
        cout << "name: " << name << endl;
        if (family == 0)
            cout << "family: SUN" << endl;
        else if (family == 1)
            cout << "family: MOON" << endl;
        else
            cout << "family: HUMAN" << endl;
        cout << "experience: " << experience << endl;
        weapon.show();
    }
    void total_num() { cout << "total number of player: " << number << endl; }
};
int Player::number = 0;

class Wizard {
private:
    float attack;
    float cure;
    Player player;
public:
    friend class Player;
    Wizard() { attack = 0; cure = 0; }
    Wizard(Player _player, float _attack = 0, float _cure = 0)
    {
        player = _player; attack = _attack; cure = _cure;
    }
    void upattack() { player.weapon.uppower(attack); }
    void upcure() { player.weapon.uplasting(cure); }
};

int main()
{
    
    //生成3种不同武器
    Weapon weapon1("dagger", 16.6, 23.3, 3);
    Weapon weapon2("axe", 30.4, 20.5, 7);
    Weapon weapon3("sword", 34.6, 41.2, 6);

    //三个玩家,两个巫师
    Player player1("Tom", weapon1, 50);
    player1.show();
    player1.upgrade(20);
    player1.show();
    Player player2("Bob", weapon3, 41);
    player2.show();
    Wizard wizard1(player2, 17.1, 22.9);
    wizard1.upattack();
    player2.show();
    Player player3("Lily", weapon2, 28);
    player3.show();
    Wizard wizard2(player3, 20.7, 11.2);
    wizard2.upcure();
    player3.show();

    Player  ps[3] = { {"Tom", weapon1, 50},{"Bob", weapon3, 41},{"Lily", weapon2, 28} };
    enum FAMILY s;
    int w = 0, e = 0, q = 0;
    for (int i = 0; i < 3; i++)
    {
        s = ps[i].getFamily();
        if (s == 0)
            w++;
        if (s == 1)
            e++;
        if (s == 2)
            q++;
    }
    cout << "SUN:" << w << endl;
    cout << "MOON:" << e << endl;
    cout << "HUMAM:" << q << endl;

    return 0;
}

巫师的attrack和cure可以帮助player升级power和lasting,为什么我在主函数里调用巫师技能最后输出显示技能发动前后玩家的power和lasting值不变呢?

  • 写回答

1条回答 默认 最新

  • 会修bug的猫 2022-12-24 17:12
    关注

    因为你传递的还是它的形参呀,当然修改不了
    传递指针进去

    class Wizard {
    private:
        float attack;
        float cure;
        Player* player;
    public:
        friend class Player;
        Wizard() { attack = 0; cure = 0; }
        Wizard(Player* _player, float _attack = 0, float _cure = 0)
        {
            player = _player; attack = _attack; cure = _cure;
        }
        void upattack() { 
            player->weapon.uppower(attack); 
        }
        void upcure() { player->weapon.uplasting(cure); }
    };
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 创建了问题 12月23日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效