0

Ok I've created the class Weapon below and I now want to create a vector of Weapons, how do I do this?

class Weapon
{
public:
    Weapon();

    // Methods
private:

    // Data members.
    std::string mWeaponName;
    int         mDamage;
    float   mCost;
};

#endif //WEAPON_H

Edited by mike_2000_17: Fixed formatting

2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by stubee_1975
0

thanks but then if I try and use wp I get the message "this declaration has no storage class or type specifier."

for example:

wp.resize(12);

0

you have to put that inside a function

#include <vector>
using std::vector;
#include "weapon.h"

int main()
{
   vector<Weapon> wp; // you can make this global if you wish
   wp.resize(12); // this must be inside some function
}
0

ok cool, so if I want to fill the vector with attributes can I do this by using simply:

wp[0] = "Sword", 10, 10.00f;

0

No, but this works

#include <vector>
#include <string>
using std::vector;

class Weapon
{
public:
    Weapon() {};
Weapon(std::string nm,int dm,float cst) 
{
    mWeaponName = nm;
    mDamage = dm;
    mCost = cst;
}


// Methods
private:

// Data members.
std::string mWeaponName;
int mDamage;
float mCost;
};

int main()
{
    vector<Weapon> wp;
    wp.resize(10);
    wp[0] = Weapon("Sword",10,10.00F);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.