Search code examples
c++classvector

Having trouble displaying Enemy data inside a vector


So, my problem is, I put the Enemy(its a constructor with parameters) object inside a vector, and when I call a function to display data of that vector, I get weird numbers.

Enemy.hpp:

#pragma once
#include <iostream>
#include <string>
using namespace std;

class Enemy {
    string name;
    int health;
    int damage;
public:
    Enemy() {};
    Enemy(string name, int health, int damage) {
        if (health < 1) {
            throw invalid_argument("Početni health ne moze biti manji od 1\n");
        }
        if (damage < 0) {
            throw invalid_argument("Enemy ne moze nanijeti negativni damage\n");
        }
    }
    void setIme(string name);
    string getIme();
    void setHealth(int health);
    int getHealth();
    void setDamage(int damage);
    int getDamage();
    void attack();
    void displayInfo();
};
Enemy.cpp:
 
#include "Enemy.hpp"

void Enemy::setIme(string name) {
    this->name = name;
}

string Enemy::getIme() {
    return name;
}

void Enemy::setHealth(int health) {
    this->health = health;
}

int Enemy::getHealth() {
    return health;
}
void Enemy::setDamage(int damage) {
    this->damage = damage;
}

int Enemy::getDamage() {
    return damage;
}

void Enemy::attack() {
    cout << "Izvrsen napad neprijatelja" << endl;
}

void Enemy::displayInfo() {
    cout << getIme() << endl;
    cout << getHealth() << endl;
    cout << getDamage() << endl;
}
main.cpp:
#include <iostream>
#include <vector>
#include "Enemy.hpp"
using namespace std;

void napad_neprijatelja(vector<Enemy>& enemies) {
    for (auto& e : enemies) {
        e.displayInfo();
        e.attack();
    }
}
int main() {
    //Enemy e;
    string ime;
    int hp, dam;
    vector<Enemy> enemies;
    int n, i;
    cout << "Unesi broj neprijatelja\n";
    cin >> n;
    for (i = 0; i < n; i++) {
        cout << "Unesi ime neprijatelja\n";
        cin >> ime;
        cout << "Unesi hp i damage\n";
        cin >> hp;
        cin >> dam;
        enemies.push_back(Enemy(ime, hp, dam));
    }
    napad_neprijatelja(enemies);
}

I tried to write only cout << ime << endl and so on in the display function but it still doesn't work. I think I cant use getters and setters and a constructor with parameters but I cant wrap my head around it.


Solution

  • To say it clear, this is useless as a constructor:

    Enemy(string name, int health, int damage) {
        if (health < 1) {
            throw invalid_argument("Početni health ne moze biti manji od 1\n");
        }
        if (damage < 0) {
            throw invalid_argument("Enemy ne moze nanijeti negativni damage\n");
        }
    }
    

    Apart from throwing exceptions in certain cases, this constructor does nothing. What you want it to do is to use the parameters to initialize the members:

    Enemy(string name_, int health_, int damage_) 
     : name(name_),health(health_),damage(damage_)
    // ^^ member initializer list
    {
       // if (...) throw ...
    }
    

    If you do not initialize health and damage members, they have indeterminate values, and trying to read from them invokes undefined behavior. The default constructor has the same issue.