以对象作为参数调用成员函数 我现在正在和我的大学一起学习中级C++课程,我们刚刚完成了课堂/多态性,所以我决定自己做一个小项目。这不是家庭作业,我想我会把它解决掉的

以对象作为参数调用成员函数 我现在正在和我的大学一起学习中级C++课程,我们刚刚完成了课堂/多态性,所以我决定自己做一个小项目。这不是家庭作业,我想我会把它解决掉的,c++,object,C++,Object,所以我用SFML做了一个“空闲”的小游戏。这包括一个实体类,一个类用于武器,一个类用于装甲。我有两个实体,一个是玩家,另一个是敌人。到目前为止,一切都很好,但当我试图用另一个实体的参数调用实体类上的成员函数时,遇到了麻烦。 下面是两个函数 /* Simulates the entity attacking another entity */ void Entity::attackEntity(Entity other) { other.decreaseHP(5); if (oth

所以我用SFML做了一个“空闲”的小游戏。这包括一个实体类,一个类用于武器,一个类用于装甲。我有两个实体,一个是玩家,另一个是敌人。到目前为止,一切都很好,但当我试图用另一个实体的参数调用实体类上的成员函数时,遇到了麻烦。 下面是两个函数

/*
Simulates the entity attacking another entity
*/
void Entity::attackEntity(Entity other) {
    other.decreaseHP(5);
    if (other.getCurrentHP() <= 0) {
        killEntity(other);
    }
}

/*
Simulates main entity defeating another
*/
void Entity::killEntity(Entity other) {
    if (other.getEntityType() == "Enemy") {
        other.setXP(rand() % (other.getRequiredXP() / 9) + 1);
        addXP(other.getXP());
        //addXP(rand() % (rand() % (getRequiredXP() / 10) + 1) + getEntityLevel()); // testing
        addGold(rand() % getEntityLevel() + getEntityLevel());

        // Increment the level of the entity to give them better armor/weapon
        other.incrementLevel();
        // Regenerate a weapon and armor for the enemy
        other.setWeapon(other.getWeapon().generateRandomWeapon(other.getEntityLevel()));
        other.setArmor(other.getArmor().generateRandomArmor(other.getEntityLevel()));
    }
    else if (other.getEntityType() == "Player") {
        other.setXP(other.getXP() / 10);
        other.setCurrentHP(other.getMaxHP());
        other.refreshEntityInfo();
    }
}
我希望代码每1秒执行一次,玩家将“攻击”另一个实体,即敌人。这将使另一个实体的生命值降低5,当另一个实体的生命值低于1时,它将“杀死”该实体,授予玩家经验并重置另一个实体的装甲和武器,这将为其提供新的统计信息。 然而,所发生的一切都是空穴来风。生命值不会减少。 显然我在这里做错了什么,因为它不起作用

我测试了在时间循环中单独调用decreaseHP()方法,结果是:

if (clock.getElapsedTime().asSeconds() >= 1.0f) {
    //player.attackEntity(enemy);
    player.decreaseHP(5);
    clock.restart();
}
但是我在使用attackEntity()方法之前提供的方法并不适用

下面是decreaseHP()方法

我是否需要传递另一个实体对象作为引用?我是否以一种糟糕的方式处理这些功能?我应该如何处理这个问题

编辑——我知道我刚刚发布了这个,但是我更改了attackEntity()函数和killEntity()函数的参数,这样它就可以引用实体对象,这似乎解决了问题

/*
Simulates main entity defeating another
*/
void Entity::killEntity(Entity &other) {
    if (other.getEntityType() == "Enemy") {
        other.setXP(rand() % (other.getRequiredXP() / 9) + 1);
        addXP(other.getXP());
        //addXP(rand() % (rand() % (getRequiredXP() / 10) + 1) + getEntityLevel()); // testing
        addGold(rand() % getEntityLevel() + getEntityLevel());

        // Increment the level of the entity to give them better armor/weapon
        other.incrementLevel();
        // Regenerate a weapon and armor for the enemy
        other.setWeapon(other.getWeapon().generateRandomWeapon(other.getEntityLevel()));
        other.setArmor(other.getArmor().generateRandomArmor(other.getEntityLevel()));
    }
    else if (other.getEntityType() == "Player") {
        other.setXP(other.getXP() / 10);
        other.setCurrentHP(other.getMaxHP());
        other.refreshEntityInfo();
    }
}

/*
Simulates the entity attacking another entity
*/
void Entity::attackEntity(Entity &other) {
    other.decreaseHP(5);
    if (other.getCurrentHP() <= 0) {
        killEntity(other);
    }
}
/*
模拟主实体击败另一个实体
*/
无效实体::killEntity(实体和其他){
if(other.getEntityType()=“敌人”){
other.setXP(rand()%(other.getRequiredXP()/9)+1);
addXP(other.getXP());
//addXP(rand()%(rand()%(getRequiredXP()/10)+1)+getEntityLevel();//测试
addGold(兰德()%getEntityLevel()+getEntityLevel());
//增加实体的等级,给他们更好的护甲/武器
other.incrementLevel();
//为敌人更新武器和盔甲
other.setLandware(other.getLandware().generaterandomLandware(other.getEntityLevel());
other.setArmor(other.getArmor().generateRandomArmor(other.getEntityLevel());
}
else if(other.getEntityType()=“Player”){
other.setXP(other.getXP()/10);
other.setCurrentHP(other.getMaxHP());
other.refreshEntityInfo();
}
}
/*
模拟攻击另一实体的实体
*/
无效实体::附件(实体和其他){
其他。降低HP(5);

如果(other.getCurrentHP()签名
void Entity::attackEntity(Entity other)
导致
other
成为该实体的副本。对
other
所做的任何更改都是
attackEntity
函数的本地更改


如果您需要从源项保留更改,最直接的方法是通过引用传入
其他
,将签名更改为:
void Entity::attackEntity(Entity&other)
签名
void Entity::attackEntity(Entity other)
使
其他
成为实体的副本。对
其他
所做的任何更改都是
附件
功能的本地更改

如果您需要从源项保留更改,最直接的方法是通过引用将
其他
传入,将签名更改为:
无效实体::附件(实体和其他)

void Entity::attackEntity(Entity other) {
…那么这个代码

Entity x;
foo.attackEntity(x);
…将创建X的副本,并将其传递给attackEntity,attackEntity将修改立即丢弃的本地副本。因此X保持不变

在这种情况下,请使用“按引用传递”

void Entity::attackEntity(Entity &other)
如果该方法不打算修改x,请改用pass by const reference:

bool Entity::isWithinRange(const Entitiy &other)
根据您的编辑,是的,引用是绝对正确的处理方法。按值传递对象(或作为指针)通常是一种代码“气味”。

鉴于此

void Entity::attackEntity(Entity other) {
…那么这个代码

Entity x;
foo.attackEntity(x);
…将创建X的副本,并将其传递给attackEntity,attackEntity将修改立即丢弃的本地副本。因此X保持不变

在这种情况下,请使用“按引用传递”

void Entity::attackEntity(Entity &other)
如果该方法不打算修改x,请改用pass by const reference:

bool Entity::isWithinRange(const Entitiy &other)

<>从你的编辑中,是的,引用绝对是正确的处理方法。按值传递对象(或指针)通常是代码“气味”。

检查你使用相等运算符,“=”。C++不使用java和C做的方式处理字符串。引用字符串(例如“敌人”)分解成指针,因此比较变得“这个指针指向的地址和另一个指针指向的地址相同吗?”答案几乎总是“否”

可以尝试使用STD::string类型,它定义了一个CuffReEE()方法,但通常,C++字符串只比C字符串稍微友好一些。


你可能应该使用旧式的C字符串比较函数,例如StrcMP-(./P>< P>)检查你使用相等运算符,“=”。C++不使用java和C做的字符串。引用字符串(例如“敌人”)分解成指针,所以比较变得“如果这个指针指向的地址与另一个指针指向的地址相同,那么答案几乎总是“否”

可以尝试使用STD::string类型,它定义了一个CuffReEE()方法,但通常,C++字符串只比C字符串稍微友好一些。


您可能应该使用老式的C字符串比较函数,例如strcmp()。

我实际上刚刚更新了OP,因为我将它改为这个,它可以工作。但是我这样做对吗?我应该使用指针吗?@Carousser,不,不要使用指针。这是错误的