为什么 rand (向量)不输出向量值,而是输出构造函数的 rand?



我试图输出一个向量的随机值,我设法做了一些类似的事情

srand(time(0));
cout << enemies[rand() % enemies.size()].getName() << endl;

为什么它不返回的值

{5, 15, 0, "name1", 40, 20},
{10, 10, 0, "name2", 60, 30},
{5, 15, 0, "name3", 20, 20},

但它确实从返回了"某物">

class Enemy : public Player
{
public:
int exp;
int money;
Enemy(int attack = 0, int defense = 0, int evasion = 0, string name = "something", int money = 0, int exp = 0)

有人能指出我在这里不理解的地方吗?我有一个类、一个构造函数和一个向量来存储数据,我想输出一个随机值,以便在遭遇战中获得随机敌人。请

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;

class Player
{
public:
int attack;
int defense;
int evasion;
string name;
Player(int atk = 0, int def = 0, int eva = 0, string nameClass = "Wiz")
{
attack = atk;
defense = def;
evasion = eva;
name = nameClass;
}
void setAttack(int atk)
{
attack = atk;
}
int getAttack()
{
return attack;
}
void setDefense(int def)
{
defense = def;
}
int getDefense()
{
return defense;
}
void setEvasion(int evs)
{
evasion = evs;
}
int getEvasion()
{
return evasion;
}
string getName()
{
return name;
}
};
class Enemy : public Player
{
public:
int exp;
int money;
Enemy(int attack = 0, int defense = 0, int evasion = 0, string name = "Wiz", int money = 0, int exp = 0)
{
}
int getAttack()
{
return attack;
}
int getDefense()
{
return defense;
}
int getEvasion()
{
return evasion;
}
string getName()
{
return name;
}
int getMoney()
{
return money;
}
int getExp()
{
return exp;
}
};

int main() 
{
srand(time(0));
Enemy timberWolf(5, 15, 0, "Timber Wolf", 40, 20);
Enemy dwarvenSharpshooter(10, 10, 0, "Dwarven Sharpshooter", 60, 30);
Enemy pharaohCat(5, 15, 0, "Pharaoh Cat", 20, 20);
Enemy swashbuckler(5, 20, 0, "Swashbuckler", 20, 10);
Enemy steelBeetle(30, 20, 30, "Steel Beetle", 160, 80);
Enemy scavengingHyena(45, 20, 30, "Scavenging Hyena", 180, 100);
Enemy manaWyrm(35, 45, 30, "Mana Wyrm", 170, 90);
Enemy manaCyclone(35, 20, 30, "Mana Cyclone", 180, 90);
Enemy pyromaniac(65, 35, 45, "Pyromaniac", 290, 160);
Enemy flamewalker(60, 50, 50, "Flamewalker", 320, 160);
Enemy arcaneAmplifier(65, 55, 45, "Arcane Amplifier", 320, 170);
Enemy voidTerror(55, 45, 55, "Void Terror", 310, 170);
Enemy mistwraith(85, 35, 65, "Mistwraith", 330, 175);
Enemy plaguebringer(75, 30, 70, "Plaguebringer", 310, 175);
Enemy henchClanBurglar(80, 35, 85, "Hench-Clan Burglar", 330, 165);
Enemy yeti(80, 40, 60, "Yeti", 320, 180);
Enemy oasisSurger(90, 45, 55, "Oasis Surger", 340, 175);
Enemy starvingBuzzard(85, 45, 60, "Starving Buzzard", 330, 190);
Enemy earthElemental(90, 45, 70, "Earth Elemental", 345, 180);
Enemy abomination(80, 55, 45, "Abomination", 340, 175);
Enemy cenarius(100, 65, 65, "Cenarius", 380, 210);
Enemy kingKrush(100, 70, 60, "King Krush", 400, 220);
Enemy archmageAntonidas(95, 65, 60, "Archmage Antonidas", 380, 210);
Enemy alAkirTheWindlord(90, 60, 65, "Al'Akir the Windlord", 360, 200);
Enemy deathWing(110, 90, 80, "Death Wing");
Player wizard(60, 40, 60, "Wizard");
Player warrior(50, 60, 40, "Warrior");
Player paladin(40, 70, 50, "Paladin");
Player druid(60, 40, 60, "Druid");
Player assassin(70, 30, 70, "Assassin");

vector<Enemy> enemies
{
{5, 15, 0, "Timber Wolf", 40, 20},
{10, 10, 0, "Dwarven Sharpshooter", 60, 30},
{5, 15, 0, "Pharaoh Cat", 20, 20},
{5, 20, 0, "Swashbuckler", 20, 10},
{30, 20, 30, "Steel Beetle", 160, 80},
{45, 20, 30, "Scavenging Hyena", 180, 100},
{35, 45, 30, "Mana Wyrm", 170, 90},
{35, 20, 30, "Mana Cyclone", 180, 90},
{65, 35, 45, "Pyromaniac", 290, 160},
{60, 50, 50, "Flamewalker", 320, 160},
{65, 55, 45, "Arcane Amplifier", 320, 170},
{55, 45, 55, "Void Terror", 310, 170},
{85, 35, 65, "Mistwraith", 330, 175},
{75, 30, 70, "Plaguebringer", 310, 175},
{80, 35, 85, "Hench-Clan Burglar", 330, 165},
{80, 40, 60, "Yeti", 320, 180},
{90, 45, 55, "Oasis Surger", 340, 175},
{85, 45, 60, "Starving Buzzard", 330, 190},
{90, 45, 70, "Earth Elemental", 345, 180},
{80, 55, 45, "Abomination", 340, 175},
{100, 65, 65, "Cenarius", 380, 210},
{100, 70, 60, "King Krush", 400, 220},
{95, 65, 60, "Archmage Antonidas", 380, 210},
{90, 60, 65, "Al'Akir the Windlord", 360, 200},
{110, 90, 80, "Death Wing"},
};
enemies.insert(enemies.begin(), 26);
cout << enemies[rand() % enemies.size()].getDefense() << endl;
return 0;
}

您的问题是没有将参数从Enemy构造函数传递到Players类。使用默认参数会隐藏此问题。如果我们删除默认参数,那么在"Enemy"构造函数中应该会出现类似的错误:

error: no matching function for call to 'Player::Player()'

由于您尚未显式调用基类构造函数,编译器正试图调用不存在的默认构造函数。解决方案是调用基类构造函数:

Enemy(int attack, int defense, int evasion, string name, int money = 0, int exp = 0)
:Player(attack, defense, evasion, name),
exp(exp),
money(money)
{
}

最新更新