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

Recommended Answers

All 6 Replies

vector<Weapon> wp;

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

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
}

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;

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

Cool - thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.