【问题标题】:How can I add different names to a vector using classes?如何使用类向向量添加不同的名称?
【发布时间】:2021-04-06 06:03:55
【问题描述】:
#include <iostream>
#include <string>
#include <vector>

class Enemy
{
private:
    std::string rank = "Boss";
    std::string rank2 = "Miniboss";
public:
    std::string type;
    std::string get_rank(){
        return rank;
        }
    std::string get_rank2(){
        return rank2;
        }
};

int add_enemy(std::vector<Enemy>&enemies, Enemy enemy) // I wanna pass by reference because I want to modify the vector
{
for(size_t i; i < enemies.size(); i++) {
    if(enemies.at(i).type == enemy.type){ // here I'm saying, if I add an enemy that's of the same type, I don't wanna add it anymore
        return 1; // it returns an error, because they are the same type, so it shouldn't add it?
        }
    }
    enemies.push_back(enemy);
    }

int main()
{
    Enemy enemy;
    enemy.type = "Dragon";
    std::cout << enemy.type << " is a " << enemy.get_rank() << std::endl;
    
    Enemy nrone, nrtwo, nrthree, nrfour, nrfive;
    // I want to add these and keep them in a vector
    std::vector<Enemy> enemies;
    
    nrone.type = "Orc";
    nrtwo.type = "Goblin";
    nrthree.type = "Troll";
    nrfour.type = "Ogre";
    nrfive.type = "Orc";
    
    std::cout << nrfour.type << " is of rank " << nrfour.get_rank2() << std::endl;
    
    enemies.push_back(nrone);
    enemies.push_back(nrtwo);
    enemies.push_back(nrthree);
    enemies.push_back(nrfour);
    enemies.push_back(nrfive);
    
    std::cout << add_enemy(enemies, enemy) << std::endl;
    
    return 0;
}

嗨,我现在正在研究 C++ 中的类和对象,我正在尝试实现以下目标:创建一个 NPC 怪物向量并将一堆怪物类型添加到向量中。但是,如果怪物/敌人属于同一类型,我不想将其添加到向量中,而是将其丢弃。

在我的例子中,我有两个兽人,所以向量应该丢弃其中一个兽人,但它没有,而是在屏幕上显示一个奇怪的数字。

我尝试过这种方式,但我仍然无法弄清楚:(有什么解决方案吗?

【问题讨论】:

标签: c++ class oop vector c++17


【解决方案1】:

所以添加两个兽人的原因是因为当您运行add_enemy 时,您已经添加了它们。所有的敌人都应该使用add_enemy函数而不是push_back

int main()
{
    Enemy enemy;
    enemy.type = "Dragon";
    std::cout << enemy.type << " is a " << enemy.get_rank() << std::endl;
    
    Enemy nrone, nrtwo, nrthree, nrfour, nrfive;
    // I want to add these and keep them in a vector
    std::vector<Enemy> enemies;
    
    nrone.type = "Orc";
    nrtwo.type = "Goblin";
    nrthree.type = "Troll";
    nrfour.type = "Ogre";
    nrfive.type = "Orc";
    
    std::cout << nrfour.type << " is of rank " << nrfour.get_rank2() << std::endl;
    
    enemies.push_back(nrone); //Add one Orc
    enemies.push_back(nrtwo);
    enemies.push_back(nrthree);
    enemies.push_back(nrfour);
    enemies.push_back(nrfive); //Add another Orc
    
    std::cout << add_enemy(enemies, enemy) << std::endl; //The Orcs are already in enemies!
    
    return 0;
}

您在屏幕上看到一个奇怪数字的原因是,如果您成功添加了一个敌人,该函数不会返回任何内容:

int add_enemy(std::vector<Enemy>&enemies, Enemy enemy) // I wanna pass by reference because I want to modify the vector
{
for(size_t i; i < enemies.size(); i++) {
    if(enemies.at(i).type == enemy.type){ 
        return 1; // Return an error
        }
    }
    enemies.push_back(enemy); //OK, so we added the enemy, but where's the return?
    }

您的add_enemies 函数必须返回一个值,因为它被声明为int 类型。

P.S...考虑使用range based loop 让事情变得更简单:

  for(Enemy& existingEnemy: enemies) {
     if(enemy.type == existingEnemy.type) {
        return 1;
     }
  }

【讨论】:

    【解决方案2】:

    这个奇怪的数字很容易解释。在你的函数中,如果你添加了敌人,你将无法返回任何东西。添加一个返回值,奇怪的数字就会消失。

    int add_enemy(std::vector<Enemy>&enemies, Enemy enemy)
    {
        for(size_t i = 0; i < enemies.size(); i++) {
            if(enemies.at(i).type == enemy.type){
                return 1;
            }
        }  
        enemies.push_back(enemy);
        return 0; // added a return value
    }
    

    两个兽人的第二个问题也很容易解释。添加兽人时,您没有使用add_enemy 函数,您只是使用了常规向量push_back 方法,因此两个兽人都被添加到向量中。你只对龙使用了add_enemy 方法。

    您也未能在循环中初始化i。我没有发现,但我已经更正了上面的代码。

    【讨论】:

      【解决方案3】:

      主要问题是您没有在 add_enemy 函数中初始化循环变量 (i)(因此循环可能永远不会运行,或者它可能 跳过一些元素)。此外,如果循环结束,该函数必须返回一个值(大概是0)。

      试试这个:

      int add_enemy(std::vector<Enemy>& enemies, Enemy enemy) // I wanna pass by reference because I want to modify the vector
      {
          for (size_t i = 0; i < enemies.size(); i++) { /// You forgot to initialize "i"!
              if (enemies.at(i).type == enemy.type) { // here I'm saying, if I add an enemy that's of the same type, I don't wanna add it anymore
                  return 1; // it returns an error, because they are the same type, so it shouldn't add it?
              }
          }
          enemies.push_back(enemy);
          return 0; // The function MUST return an int value!
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-20
        • 2021-02-16
        • 2010-12-28
        • 2022-11-16
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多