我想制作一个向量,其中类型是我的重载构造函数,或者确切地说,我想制作一个怪物向量,但我似乎无法通过它,我也不知道为什么。我做错了什么?

// Monster.cpp

#include <iostream>
#include <string>
#include "Monster.h"
#include "Player.h"
#include "Random.h"
#include "Weapon.h"
using namespace std;

Monster::Monster(const std::string& name, int hp, int acc,
    int xpReward, int armor, const std::string& weaponName,
    int lowDamage, int highDamage, int monstergold)
{
    mName = name;
    mHitPoints = hp;
    mAccuracy = acc;
    mExpReward = xpReward;
    mArmor = armor;
    mWeapon.mName = weaponName;
    mWeapon.mDamageRange.mLow = lowDamage;
    mWeapon.mDamageRange.mHigh = highDamage;
    mGold = monstergold;
}




这是地图,如果掷骰高于20,则应该遇到一群怪物

else if (roll > 20)
{
    vector <Monster(const std::string& name, int hp, int acc,int xpReward, int armor, const std::string& weaponName, int lowDamage, int highDamage, int monstergold)> MonsterArray;
    MonsterArray.push_back("Orc Lord", 25, 15, 2000, 5,"Two Handed Sword", 5, 20, 100);


    cout << "You encountered an multiple monsters!!!" << endl;
    cout << "Prepare for battle!" << endl;
    cout << endl;
}




错误是,它说没有重载功能。我知道这是错误的,但我真的不知道该怎么做。有什么建议吗?

最佳答案

您需要在模板中指定typename,而不是尝试调用构造函数:

// Here you define that vector will contain instances of Monster class
vector<Monster> MonsterArray;

// Add new monster by explicitly calling
// to the constructor and pushing into container
MonsterArray.push_back(Monster("Orc Lord", 25, 15, 2000, 5,"Two Handed Sword", 5, 20, 100));


虽然我建议读The C++ Programming Language书。



而且似乎您缺少向量容器的包含,例如:

#include <vector>

关于c++ - 怪物的C++ vector ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45561096/

10-11 18:47