为啥我不能在 C++ 中更新我的类属性?

Posted

技术标签:

【中文标题】为啥我不能在 C++ 中更新我的类属性?【英文标题】:Why can't I update my class properties in C++?为什么我不能在 C++ 中更新我的类属性? 【发布时间】:2021-09-05 09:44:06 【问题描述】:

我正在制作一个向导游戏原型,但遇到了一个问题:

我创建了一个名为 Wizard 的类和一个名为 Goblin 的类:

class Wizard

    public:
        int damage;
        int health;
        int stamina;

        Wizard(int d, int h, int s)
        
            d = damage;
            h = health;
            s = stamina;
        

        float getDamage()  return damage; 
        void setDamage(int v)  v = damage; 

        float getHealth()  return health; 
        void setHealth(int v)  v = health; 

        float getStamina()  return stamina; 
        void setStamina(float v)  v = stamina; 

;

class Goblin

    public:
        int health;
        int damage;

        Goblin(int h, int d)
        
            h = health;
            d = damage;
        

        float getHealth()  return health; 
        void setHealth(int v)  v = health; 

        float getDamage()  return damage; 
        void setDamage(int v)  v = damage; 
;

然后,我为它们声明并初始化变量:

Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);

然后,我创建了一个attack() 函数:

void attack(Wizard attacker, Goblin target)

    if(target.getDamage() > 0.0)
    
        attacker.setStamina(attacker.getStamina() - 1);
    

所以,在主函数中,我调用了attack并打印出结果:

int main()

    attack(wizard, goblin);
    printInfo();

    return 0;

但结果是这样的:

> clang++-7 -pthread -std=c++17 -o main main.cpp
> ./main
Wizard Health: 0
Wizard Damage: 0
Wizard Stamina: 0

Goblin Damage: 0
Goblin Health: 0

我还创建了一个printInfo() 函数:

void printInfo()

    std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
    std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
    std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
    
    std::cout << std::endl;

    std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
    std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;

如果你需要,这里是完整的文件:

#include <iostream>

class Wizard

    public:
        int damage;
        int health;
        int stamina;

        Wizard(int d, int h, int s)
        
            d = damage;
            h = health;
            s = stamina;
        

        float getDamage()  return damage; 
        void setDamage(int v)  v = damage; 

        float getHealth()  return health; 
        void setHealth(int v)  v = health; 

        float getStamina()  return stamina; 
        void setStamina(float v)  v = stamina; 

;

class Goblin

    public:
        int health;
        int damage;

        Goblin(int h, int d)
        
            h = health;
            d = damage;
        

        float getHealth()  return health; 
        void setHealth(int v)  v = health; 

        float getDamage()  return damage; 
        void setDamage(int v)  v = damage; 
;

Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);

void attack(Wizard attacker, Goblin target);
void printInfo();

int main()

    attack(wizard, goblin);
    printInfo();

    return 0;


void attack(Wizard attacker, Goblin target)

    if(target.getDamage() > 0.0)
    
        attacker.setStamina(attacker.getStamina() - 1);
    


void printInfo()

    std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
    std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
    std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
    
    std::cout << std::endl;

    std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
    std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;

很抱歉,如果这很长,但我们将不胜感激。

【问题讨论】:

在您将h 分配给health 的构造函数中,它应该是相反的。其他领域也是如此。 在设置器中存在与 Raildex 提到的问题相同的问题。我习惯在成员变量前加上m_,这可能有助于避免混淆。顺便说一句:为什么你的吸气剂返回float?成员变量具有 int 类型,setter 也采用 int... 【参考方案1】:

您的attack() 函数按值接受参数。因此,它会修改您的全局变量的副本。

使用参考:

void attack(Wizard& attacker, Goblin& target)

    ...

PS:另见 https://en.cppreference.com/w/cpp/language/constructor 了解成员初始化列表

【讨论】:

您可能想提及在设置器/构造器中以“反向”完成分配的问题... 一次一节课?【参考方案2】:

在您的构造函数和 set* 方法中,您需要将输入参数值分配给类的成员变量。反之亦然。

#include <iostream>

class Wizard

    public:
        int damage;
        int health;
        int stamina;

        Wizard(int d, int h, int s)
        
            damage = d;
            health = h;
            stamina = s;
        

        float getDamage()  return damage; 
        void setDamage(int v)  damage = v; 

        float getHealth()  return health; 
        void setHealth(int v)  health = v; 

        float getStamina()  return stamina; 
        void setStamina(float v)  stamina = v; 

;

class Goblin

    public:
        int health;
        int damage;

        Goblin(int h, int d)
        
            health = h;
            damage = d;
        

        float getHealth()  return health; 
        void setHealth(int v)  health = v; 

        float getDamage()  return damage; 
        void setDamage(int v)  damage = v; 
;

Wizard wizard(50, 150, 10);
Goblin goblin(150, 25);

void attack(Wizard attacker, Goblin target);
void printInfo();

int main()

    attack(wizard, goblin);
    printInfo();

    return 0;


void attack(Wizard attacker, Goblin target)

    if(target.getDamage() > 0.0)
    
        attacker.setStamina(attacker.getStamina() - 1);
    


void printInfo()

    std::cout << "Wizard Health: " << wizard.getHealth() << std::endl;
    std::cout << "Wizard Damage: " << wizard.getDamage() << std::endl;
    std::cout << "Wizard Stamina: " << wizard.getStamina() << std::endl;
    
    std::cout << std::endl;

    std::cout << "Goblin Damage: " << goblin.getHealth() << std::endl;
    std::cout << "Goblin Health: " << goblin.getDamage() << std::endl;

【讨论】:

谢谢,但 Ralidex 已经回答了。这些值正在打印,但未更新。

以上是关于为啥我不能在 C++ 中更新我的类属性?的主要内容,如果未能解决你的问题,请参考以下文章

为啥元类不能访问由元类定义的类的子类继承的属性?

C++ 中的类属性变量

为啥我不能更新核心数据实体属性的布尔值?

为啥定义没有 CUDA __device__ 属性的类头有效? (C++)

当我尝试从文本文件输入值时,为啥我的类列表(在 C++ 中)会引发错误?

为啥我无法访问 Angular 组件的传递函数内的类属性?