(C++) 无法让 deque insert() 工作,我做错了什么?
我在此处的 insert()
部分遇到错误,我不知道我做错了什么。我尝试了不同的参数和参数数量,但似乎没有任何效果。
m_oGameObjectList
是 IGameObjects(基类)的双端队列。m_sPosition
是一个具有 3 个整数(X、Y、Z)的结构。gameObject
是对从 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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
insert
采用一个迭代器
。使用:您还需要在
deque
中使用指针,现在您正在切片 - 插入gameObject
的IGameObject
部分的副本>insert
takes aniterator
. Use:You'll also need to use pointers in your
deque
, right now you're slicing - inserting a copy of theIGameObject
part ofgameObject
对
insert
的调用应将迭代器(而不是整数索引)传递到双端队列中。将整数索引转换为双端队列迭代器的一种方法是通过:...尽管还有其他几种同样有效的解决方案。
您还可以使用像
push_back
和push_front
这样的双端队列函数,它们只分别获取要放在双端队列前面或后面的对象。Your call to
insert
should pass an iterator (not an integer index) into the deque. One way you can convert an integer index to a deque iterator is via:... though there are several other solutions that work equally well.
You can also use deque functions like
push_back
andpush_front
which just take the object you want to put at the front or back of the deque, respectively.尝试将从 IGameObject 派生的对象插入到 deque中;不起作用,因为双端队列试图在引用中存储对象的副本,而不是引用本身。
大多数时候,如果您尝试将类层次结构存储到容器中,则可以通过使用指向基类的指针的容器来实现。
trying to insert an object derived from IGameObject into a deque<IGameObject> won't work as the deque is trying to store a copy of the object in the reference, not the reference itself.
Most of the time, if your trying to store an class hiearchy into a container, you do so by having the container of pointers to the base class.