I'm getting confused with making vector functions work when using a vector of objects. push_back(); is not a member function of TheObject, so when I try to do something like this:
Calling the vector TheObject is kinda strange, because it is a vector of Objects.
TheObject.push_back();
As vlad said, this would expect an argument of type Object like:
1 2
Object anObject;
TheObject.push_back(anObject);
Though if that's exactly what it said in the book, it was probably just an example and not meant to be actually compiled. For example, this would require the existence of a class "Object" with a member function called "SetThings".
Maybe I'm doing the wrong thing then? I'm trying to cross over from using arrays to vectors. So I want to convert the following into vector format:
1 2 3 4 5 6 7
Object Things[5];
Things[0].SetStuff(1,1,1,1);
Things[1].SetStuff(2,2,2,2);
// in a vector:
// vector<Object>Things(0);
// Things.push_back(); (create the first object?)
// Things[0].SetStuff(1,1,1,1);
My problem is that when I call Object.push_back();, I want to create what is essentially an array index which I can set values to. The program thinks I'm trying to call a function of the Object class though, when I'm actually trying to call a function of vector. Here is the code I wrote when testing in my actual program:
The purpose of a vector is to store multiple objects in one place - push_back() tells the vector to push an object to the back; in order to do that, you need to create an object to push;
e.g.
1 2 3 4 5 6 7 8 9 10
vector<Object> things;
Object fred;
Object harry;
things.push_back( fred ); // push first object, will be position 0
things.push_back( harry ); // push second object, will be position 1
things[0].SetStuff( 1,1,1,1 ); // [0] stores a copy of 'fred'
things[1].SetStuff( 2,2,2,2 ); // [1] stores a copy of 'harry'
push_back is a function which belongs to the vector, so the variable on the left-hand-side of your . needs to be the name of the vector (without the subscript operator). When you use the subscript operator, you're treating it like an array (i.e. accessing the contained objects).
vector::push_back means "Grab a copy of this object and shove it in the next empty/available block of memory belonging to the vector".
If you want to create a vector which already contains a bunch of initialised objects (i.e. you know how many objects you want at the time you create it), then it's easier to specify the size when you create it
1 2 3 4
vector<Object> things( 2 );
things[0].SetStuff( 1,1,1,1 ); // [0] was already initialised by vector
things[1].SetStuff( 2,2,2,2 ); // [1] was already initialised by vector
Ah right I understand now.. dumb thinking on my part. Thanks for the help Bench and Peter. Now I just need to work out how to delete objects in the middle of the vector, not sure if deques would be more useful for that since they can delete the front too, but I'll mess around with that for a bit myself.
Uh, gonna bring this back for a minute while it's still kinda here. I got everything working perfectly with the vector (after some messing around, phew), but I can't get this function to work any more. You guys will probably have a tough time helping me since this code is in the midst of a few classes etc, but here goes anyway:
1 2 3 4 5 6 7 8 9 10 11
void Actors :: AddSkill(Skills NewSkill)
{
if(KnownSkills.size() > 4){ // 4 = max skills, KnownSkills = the vector
DeleteSkill(KnownSkills); // 'DeleteSkill' not declared in this scope, it's a Skills class function
}
KnownSkills.push_back(NewSkill);
SkDisplay++;
}
// DeleteSkill is declared as:
//void Skills :: DeleteSkill(vector<Skills>KnownSkills)
This worked before when I used an array, any help?
a deque is a good choice if you don't care about the underlying arrangement in memory, although there's nothing inherently wrong with using vector either.
If you want to erase the first element from a vector, then you can do so using KnownSkills.erase( KnownSkills.begin() )
The 'erase' function accepts an iterator; 'vector::begin()' returns an iterator which references the first object in the vector. (Iterators are an alternative to using indexes to access objects. Iterators are usually more useful than index values since they can be passed around to other functions which let you step through the vector - for example, the standard library has got a bunch of standard functions such as find/replace/reverse/etc which all use iterators)
What do you intend 'DeleteSkill' to do? If your Actors class owns a vector<Skill> and you want to delete something from that vector, then really 'DeleteSkill' sounds like it should be a part of the Actors class rather than the Skill class. (The Skill class shouldn't really need to know anything about the vector)