我想制作一个矢量,其中类型是我的重载构造函数,或者准确地说我想制作一个怪物矢量,但我似乎无法通过它,我不知道为什么。我做错了什么?
// 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;
}
错误是,它表示没有重载功能。我知道这是错的,但我真的不知道该怎么做。有什么建议吗?
答案 0 :(得分:4)
您需要在模板中指定 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>
答案 1 :(得分:0)
在声明向量时,您无需指定Monster类的所有参数。 简单地做这样的事情:
std::vector<Monster> monsters;
monsters.push_back(new Monster("Orc Lord", 25, 15, 2000, 5,"Two Handed Sword", 5, 20, 100));