dimanche 29 août 2021

problem with operator[] when trying to access a vector of shared_ptr

I have the following class :

class Character
{
     /unimportant code
}
class Fighter : public Character
{
     /unimportant code
}
class Healer: public Character
{
     /unimportant code
}


class Game
{
public:
    void move(const GridPoint & src_coordinates,  const GridPoint & dst_coordinates);
    //there are more things here ,but they're not important for the sake of this question. 
private:
    int height;
    int width;
    std::vector<std::shared_ptr<Character>> gameboard;
};


void move(const GridPoint & src_coordinates,  const GridPoint & dst_coordinates)
{
    for (std::vector<std::shared_ptr<Character>>::iterator i = gameboard.begin(); i != 
                                                                  gameboard.end() ; i++ )
    {
        if ( (*gameboard[i]).coordinates == src_coordinates)
        {
            //do I need to implement my own [] operator?
        }
           
        
    }
        
}

Im trying to iterate over my gameboard and move the character from src_coordinates to dst_coordinates. Character is also a class that's inherited by a few more :

I get the following error when I try to access the elements of gameboard[i] :

no match for 'operator[] (operand types are 'std::vector<std::shared_ptr<Character> >' and 'std::vector<std::shared_ptr<Character> >::iterator' {aka '__gnu_cxx::__normal_iterator<std::shared_ptr<Character>*, std::vector<std::shared_ptr<Character> > >'}

does that mean that I have to implement my own operator[] and operator* because Character is a class of my own? and how can I solve my ptoblem?

Aucun commentaire:

Enregistrer un commentaire