Search code examples
c++insertdeque

(C++) Can't get deque insert() to work, what am I doing wrong?


I'm getting an error on the insert() part here, and I dunno what I am doing wrong. I've tried different parameters and number of parameters but nothing seems to work.

m_oGameObjectList is a deque of IGameObjects (base class).
m_sPosition is a struct with 3 ints (X, Y, Z).
gameObject is the reference to an object derived from IGameObject.

for (int i = 0; i < m_oGameObjectList.size(); i++)
{
    if (gameObject.m_sPosition.Z > m_oGameObjectList[i].m_sPosition.Z)
    {
        m_oGameObjectList.insert(i, gameObject);
        i = m_oGameObjectList.size();
    }
}

Solution

  • insert takes an iterator. Use:

    m_oGameObjectList.insert(m_oGameObjectList.begin() + i, gameObject);
    

    You'll also need to use pointers in your deque, right now you're slicing - inserting a copy of the IGameObject part of gameObject