Luigi Istratescu
Luigi Istratescu

Reputation: 97

How can I add different names to a vector using classes?

#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;
}

Hi, I am studying Classes & Objects in C++ right now, and I'm trying to achieve the following: create a vector of NPC monsters and add a bunch of monster types to the vector. However, if the monster/enemy is of the same type, I don't want to add it to the vector, but discard it.

In my case, I have two Orcs, so the vector should discard one of the orcs, but it doesn't, and instead if showing me a strange number on the screen.

I tried it this way and I still can't figure it out :( Any solutions?

Upvotes: 0

Views: 135

Answers (3)

Brad
Brad

Reputation: 2320

So the reason that both Orcs are added is because by the time you run add_enemy, you've already added them. All the enemies should be using the add_enemy function instead of 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;
}

The reason you're seeing a strange number on the screen is that if you DO successfully add an enemy, the function doesn't return anything:

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?
    }

Your add_enemies function must return a value, since it is declared as type int.

P.S... consider using a range based loop to make things a little easier:

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

Upvotes: 1

john
john

Reputation: 87959

The strange number is easily explained. In your function you fail to return anything in the case where you do add the enemy. Add a return value and the strange number will go away.

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
}

The second problem with two orcs is also easily explained. You didn't use your add_enemy function when you added the orcs, you just used the regular vector push_back method so both orcs got added to the vector. You only used your add_enemy method for the dragon.

Also you fail to initialise i in the loop. I didn't spot that but I've corrected the code above.

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51815

The main problem is that you are not initializing the loop variable (i) in your add_enemy function (so the loop may never run, or it may skip some elements). Also, that function must return a value (presumably, 0) if the loop ends.

Try this:

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!
}

Upvotes: 1

Related Questions