Hi, I am writing a program for my C++ class that must create a struct(Hero) and make an array of it. My class is using the lvp library and i was wondering if anybody could help me make a vector of hero objects.
This is what i have so far:

#include<iostream.h>
#include<lvp\string.h>
#include<lvp\vector.h>
#include<lvp\random.h>
struct Hero
{
       Hero();
       Hero(String n, String c, String r, int h, int s, int d, int co, int w, int ch, int i,vector<String> e);
       Hero(String n, String c, String r);
       String Name;
       String Class;
       String Race;
       int HitPoints;
       int Strength;
       int Dexterity;
       int Constitution;
       int Wisdom;
       int Charisma;
       int Intelligence;
       vector<String> Equipment;
       int NumItems;
};
Hero::Hero()
:Name(""),Class(""),Race(""),HitPoints(1+random(20)),Strength(1+random(20)),Dexterity(1+random(20)),Constitution(1+random(20)),Wisdom(1+random(20)),Charisma(1+random(20)),Intelligence(1+random(20))
{
}
Hero::Hero(String n, String c, String r, int h, int s, int d, int co, int w, int ch, int i,vector<String> e)
:Name(n),Class(c),Race(r),HitPoints(h),Strength(s),Dexterity(d),Constitution(co),Wisdom(w),Charisma(ch),Intelligence(i)
{
     Equipment.resize(e.length());
     for(int i=0;i<Equipment.length();i++)
             Equipment[i]=e[i];
     NumItems=Equipment.Length;
}
Hero::Hero(String n, String c, String r)
:Name(n),Class(c),Race(r),HitPoints(1+random(20)),Strength(1+random(20)),Dexterity(1+random(20)),Constitution(1+random(20)),Wisdom(1+random(20)),Charisma(1+random(20)),Intelligence(1+random(20))
{
}       
            
int main()
{
    randomize();
    return(0);
}

Recommended Answers

All 2 Replies

Don't you already do it with 'Equipment' ?

You have several options on how to store the Hero object in the vector, you can store it like you store the "String" in Equipment, or you can decide to store a pointer to a Hero object (Hero*) instead.. it depends on the requirements of the assignment what to chose.

I don't know the lvp library, but with the standard library the vector class has a member function "push_back" which allows you to add elements to the vector. (e.g. vHeroArray.push_back( new Hero() ); if you want to store hero pointers)

Don't you already do it with 'Equipment' ?

No, I do not do it with 'Equipment'. That is storing the string of equipment for a given hero but I understand what you mean. With lvp I think the thing closest to what your saying is the user built resize function which looks like this:

void AddToArray(vector<String> &Array, const String &ValueToAdd)
{
Array.resize(Array.length()+1)
Array[Array.Length()-1]=ValueToAdd
}

Thank you very much for your input.

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.