0

I want to make a vector where the type is my overloading constructor or to be precise I want to make a vector of monsters but I cant seem to pass it and I have no idea why. What did I do wrong?

// 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;
}

this is the map , if the roll was higher than 20, it should encounter a group of monsters

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;
}

the error was, it says that no overloaded function. I know this is wrong but I just really have no idea how to do it. any advice?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Ivan Apungan
  • 534
  • 6
  • 17
  • I think you need to [read a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn how to use template classes. – Some programmer dude Aug 08 '17 at 06:47
  • 1
    You should probably also learn about the [`std::vector::emplace_back`](http://en.cppreference.com/w/cpp/container/vector/emplace_back) function. And read more about [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) in general. – Some programmer dude Aug 08 '17 at 06:48

2 Answers2

4

You need to specify typename in the template rather than trying to call to the constructor:

// 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));

While I'd suggest to read The C++ Programming Language book.


Also it seems that you are missing include of the vector container, e.g.:

#include <vector>
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
  • oh my god, that works, welp that was easy, I have a question, what is the diffrence between "Monster" inside the "<>" of the vector compared to the "Monster()" , I know that the "Monster()" is a constructor but I'd like to know what is their difference and uses. Example I have a class "Player" then there is also a constructor "Player()" I declared a Player type outside the class, so it should go like this, Player mainPlayer, this is a object right? In this case, I am not adding "()" to the vector so does that mean I am declaring a group of object? – Ivan Apungan Aug 08 '17 at 06:53
  • I'd strongly recommend to read the book. When you are using templates then in <> braces you usually (depends on template) define the typename to substitute into template parameter. Now, then you defining Player mainPlayer; it implicitly calls to the default constructor. But to be precise there are much more than I can capture in the comment and it cannot replace the book. – Artem Barger Aug 08 '17 at 06:58
  • 1
    When you have a class that has a default constructor and you declare a variable of that type, that is what you have a variable or object either on the stack or in heap in most cases. Even when you see code like: `Player player;` by default it is calling `Player player();` now when you see a class's typename within template parameters such as your basic containers `std::vector` or `std::vector` or even pointers to them: `std::vector` or `std::vector` these are not instances of the class, these are typenames or pointers to a type. – Francis Cugler Aug 08 '17 at 09:08
0

You don't need to specify all parameters of your Monster class while declaring your vector. Simply do something like that :

std::vector<Monster> monsters;
monsters.push_back(new Monster("Orc Lord", 25, 15, 2000, 5,"Two Handed Sword", 5, 20, 100));