簡體   English   中英

為什么 rand (向量的)不輸出向量值,而構造函數的呢?

[英]Why doesn't rand (of a vector) output a vector value, but does of the constructor?

我試圖輸出一個向量的隨機值,我設法做類似的事情

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)
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM