samedi 28 août 2021

problem with [] operator when using a vector

I'm trying to make a copy constructor for Game. throughout the copy constructor I have to copy the elements of one game into another. However when I try to access the inner elemnts of the game I want to copy I get an error saying :

no operator "[]" matches these operands -- operand types are: mtm::Game [ std::_Vector_const_iterator<std::_Vector_val<std::conditional_t<true, std::_Simple_types<std::vector<char, std::allocator>>, std::_Vec_iter_types<std::vector<char, std::allocator>, size_t, ptrdiff_t, std::vector<char, std::allocator> *, const std::vector<char, std::allocator> *, std::vector<char, std::allocator> &, const std::vector<char, std::allocator> &>>>> ]C/C++(349)

I'd appreciate any help with explaining why the [] operator doesn't work, here's the piece of code I wrote :

Game::Game(const Game& other)
{
    Game game(other.height, other.width);

    for (vector<vector<char>>::const_iterator row = other.game.begin(); row != 
                                                              other.game.end(); row++)
    {
        for(vector<char>::const_iterator col = row->begin(); col != row->end(); col++)
        {                
            game[row][col] = other[row][col];  ///////???
        }
    }

In addition to that, I'd like to ask if it's better to allocate a game using "new" or just declare it like I did in my code segment above.

Aucun commentaire:

Enregistrer un commentaire