vector在没有交互的情况下被清除
std::vector cleared without interaction
我有一个std::vector
被清除的问题,但我不知道在哪里。我花了很多时间调试这个问题,但我一辈子也找不到它是在哪里被清除的。
class ENGINE_API QuadTree {
private:
typedef std::unordered_set<QuadTreeObject*> EntityContainer;
typedef std::vector<QuadTree> ChildContainer;
const static int MAX_OBJECTS = 5;
AABB bounds;
ChildContainer children;
EntityContainer entities;
EntityContainer allEntities;
// Internal methods
void split();
int getIndex(const AABB&)const;
template <class T>
void merge(T& one, const T& two) {
one.insert(two.begin(), two.end());
}
// End Internal methods
public:
QuadTree(AABB bounds) : bounds(bounds), children(), entities(), allEntities() {
children.reserve(4);
}
// Base
void clear() {
entities.clear();
allEntities.clear();
children.clear();
}
void insert(QuadTreeObject*);
void remove(QuadTreeObject*, const AABB&);
// Not the source, I've removed this and it still works fine (Nothing uses this method)
ChildContainer& getChildren() {
return children;
}
AABB& getBounds() {
return bounds;
}
QuadTree getLowestRoot(const AABB& aabb)const {
int index = getIndex(aabb);
if (index == -1 || children.empty()) {
return *this;
}
return children.at(index).getLowestRoot(aabb);
}
const EntityContainer& getAll()const {
return allEntities;
}
// End Base
};
void QuadTree::split() {
children.clear(); // supposed to be there
double width = (bounds.getWidth() / 2);
double height = (bounds.getHeight() / 2);
children.push_back(QuadTree(AABB(bounds.min.x + width, bounds.min.y, width, height)));
children.push_back(QuadTree(AABB(bounds.min.x, bounds.min.y, width, height)));
children.push_back(QuadTree(AABB(bounds.min.x, bounds.min.y + height, width, height)));
children.push_back(QuadTree(AABB(bounds.min.x + width, bounds.min.y + height, width, height)));
auto it = entities.begin();
while (it != entities.end()) {
int index = getIndex((*it)->getAABB());
if (index != -1) {
children.at(index).insert(*it);
it = entities.erase(it);
}
else {
it++;
}
}
}
int QuadTree::getIndex(const AABB& aabb)const {
// Fits in top
bool topQuadrant = aabb.max.y < aabb.horzMid;
// Fits in botom
bool bottomQuadrant = aabb.min.y > aabb.horzMid;
// Fits in left
if (aabb.max.x < aabb.vertMid) {
if (topQuadrant) {
return 1;
}
else if (bottomQuadrant) {
return 2;
}
}
// Fits in right
else if (aabb.min.x > aabb.vertMid) {
if (topQuadrant) {
return 0;
}
else if (bottomQuadrant) {
return 3;
}
}
return -1;
}
void QuadTree::insert(QuadTreeObject* object) {
AABB aabb = object->getAABB();
allEntities.insert(object);
if (children.empty()) {
if (entities.size() <= MAX_OBJECTS) {
entities.insert(object);
return;
}
split();
}
int index = getIndex(aabb);
if (index != -1) {
children.at(index).insert(object);
}
else {
entities.insert(object);
}
}
void QuadTree::remove(QuadTreeObject* object, const AABB& aabb) {
if (!allEntities.erase(object)) {
return;
}
if (!entities.erase(object)) {
if (!children.empty()) {
int index = getIndex(aabb);
if (index != -1) {
children.at(index).remove(object, aabb);
}
else {
throw std::logic_error("Can't be in allEntities but not entities!");
}
}
else {
throw std::logic_error("Can't be in allEntities but not entities!");
}
}
}
当调用split
方法时,children.size()
返回4,但当它到达insert
时,它又返回0。这怎么可能呢?
这是一个问题:
class ENGINE_API QuadTree {
typedef std::vector<QuadTree> ChildContainer;
ChildContainer children;
在类定义完成之前, QuadTree
是一个不完整类型。如果您使用不完整类型的std::vector
,则行为是未定义的。
换句话说,类不能包含自身的向量。你将不得不重新设计你的类,例如:
- 切换为智能指针向量,
- 用pImpl语言实现向量
- 使用不同的容器,该容器设计用于不完整类型
相关文章:
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- libQGLViewer 如何在没有清除缓冲区的情况下绘制
- SDL2:如何在不清除屏幕的情况下渲染
- 如何在没有递归的情况下清除四叉树(也许使用队列?).
- 为什么回调标志在没有明显原因的情况下被清除和/或损坏
- 在不删除部件的情况下清除布局
- 如何输出到控制台,并在不清除屏幕和重写所有内容的情况下覆盖文本
- vector在没有交互的情况下被清除