Vector member functions messing up

I am making a game, and I have a vector<string> equippedArmor in class character. One of the member functions of character is character::recalcStats, which loops through the player's equipped items to figure out how much damage he does, how much armor he has, any attribute bonuses/detriments, etc.

In this function, I have an int iter, which I set to equippedArmor.begin(); Then this error pops up: error: request for member 'begin' in '((character*)this)->character::equippedArmor', which is of non-class type 'std::string [6]'

Coincidentally, equippedArmor has six objects in it, but it is filled up in the constructor of character.

I have tried iterators, but I got the same problem. It seems the problem is in the begin() member function, but I have no idea how to fix it, please aid me with this.

Thank you!
TsarLenin wrote:
I have an int iter, which I set to equippedArmor.begin();

You cannot assign an iterator to an integer. Change it to:

vector<string>::iterator iter;

Also, could you please post your code (in code tags please). It is probably the only way to receive help at the moment.
Firstly, iter is not actually an iterator, I simple named it iter because it iterates through the vector, it is actually an int. The way I use it is I send equippedArmor[iter] to a function, recalcArmor(), which checks that specific object for components that will modify the users stats. The iterating through equippedArmor happens in a recalcStats, which is essentially a driver.

Here it is:

1
2
3
4
5
6
7
8
9
10
11
void character::recalcStats()
{
     int iter;
     iter = equippedArmor.begin();

    do
    {
          character::recalcArmor(character::equippedArmor[iter]);
          iter += 1;
     }while( iter != character::equippedArmor.end());
}

TsarLenin wrote:
Firstly, iter is not actually an iterator,

In your code it is.

equippedArmor.begin()
returns an iterator object of type:
vector<string>::iterator
and thus cannot be casted to an int.

your simplest change would be to change line 4 to:
iter=0;

and line 10 to:

while(iter<character::equippedArmor.size());
Last edited on
http://www.cplusplus.com/reference/vector/vector/begin/

vector::begin returns an interator pointing to the first element in the vector but you are trying to assign it to an int. You need to choose between an iterator and an int then loop over your vector with a for or while loop. I wouldn't use a do/while because this always executes at least once and if the equippedArmor vector is empty it will just crash when it tries to access the first element.

1
2
3
4
5
6
7
std::string::iterator iter = equippedArmor.begin();

while ( iter != equippedArmor.end() )
{
    // do whatever calculations you need to do
    iter++;
} 


or

1
2
3
4
5
6
7
int index = 0;

while ( index < equippedArmor.size() )
{
    // do whatever calculations you need to do
    index++;
} 


I've kept them as while loops since it's closer to your original code but a for loop is what I would normally use.
Last edited on
I, believe or not, have tried both, but the compiler gave me the above stated "request for..." error, except with either size() or begin() and end(). I could, perhaps, limit the vector size (or use an array) and have specific equipment spots, but I would like to know a solution to this problem so that, if I ever have to use a vector in a situation like this, I can do it without difficulties.
From what you have posted both solutions should work. You should post more code if you want us to be able to help you further.
Alright!

The class itself in main.cpp:
1
2
3
4
5
6
7
8
9
10
11
class character
{
     //Only notable functions and vector are presented, the class is actually quite big!

     character(); //The constructor, equippedArmor is filled in this function.

     vector<string> equippedArmor;

     void recalcStats(); //The driver for recalculations
     void recalcArmor( string ); //Stat recalculations for armors
};


The constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
character::character()
{
     /*This is character creation, based on user input character stats are modified and
     starting equipment is determined (so, equippedArmor is filled up).*/
     
     //In this case equipment is filled out for the wizard class
     character::equippedArmor[0] = ("Worn Cloth Robe");
     character::equippedArmor[1] = ("Worn Cloth Pants");
     character::equippedArmor[2] = ("Tattered Leather Travelling Shoes");
     character::equippedArmor[3] = ("Empty");
     character::equippedArmor[4] = ("Empty");
     character::equippedArmor[5] = ("Empty");     
}


//character::recalcStats() - the driver
1
2
3
4
5
6
7
8
9
10
11
void character::recalcStats()
{
     int iter;
     iter = equippedArmor.begin();

    do
    {
          character::recalcArmor(character::equippedArmor[iter]);
          iter += 1;
     }while( iter != character::equippedArmor.end());
}


//character::recalcArmor(string) - calculates armor bonuses
1
2
3
4
5
6
7
8
9
10
void character::recalcArmor( string armorPiece )
{
     character::armor = 0;

     if( armorPiece.find( "Tattered" ) != string.npos )
     {
          character::armor -= 2;
     }
     //It goes on to check for other keywords.
}
I have decided to put a limit on equipable armor (seems only logical) and make equippedArmor an array. I have no idea what was going on, but the program can now continue! Thank you for your input anyway!
Firstly, it looks like your constructor will cause a segmentation fault. Rather do something like:
character::equippedArmor.push_back("Worn Cloth Robe");

Also you cannot use an iterator on an array. And you should really try and use C++ style vectors over C style arrays where possible. (At least that is what I have been told). But good luck, I hope you come right.
equippedArmor used to be a vector, I then changed it to an array to avoid this "request" error. The size() function works for other vectors in other functions, though. I have no idea what went wrong, but the problem is solved. Thank you all for your input!
Topic archived. No new replies allowed.