我正在库中实现四叉树,并且编译器不断抛出有关不完整类型的错误:quadtree.h
template<int capacity,
typename t,
typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
struct quadtree {
bool divided = false;
quadtree<capacity, t>* nw,* ne,* sw,* se;
std::vector<t> objs;
rect2 b;
quadtree(rect2 bounds): b(b) {}
void addObj(t);
void divide();
void assign(t);
void empty();
};
bin.cpp
template<int capacity,
typename t,
typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
void quadtree<capacity, t>::addObj(t o) {
if(!divided) {
objs.push_back(o);
if(objs.size() > capacity) {
divide();
}
} else {
assign(o);
}
}
template<int capacity,
typename t,
typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
void quadtree<capacity, t>::divide() {
divided = true;
nw = new quadtree<capacity, t>(rect2(b.x(), b.y(), b.w()/2, b.h()/2));
ne = new quadtree<capacity, t>(rect2(b.x()+b.w()/2, b.y(), b.w()/2, b.h()/2));
sw = new quadtree<capacity, t>(rect2(b.x(), b.y()+b.h()/2, b.w()/2, b.h()/2));
se = new quadtree<capacity, t>(rect2(b.x()+b.w(), b.y()+b.h(), b.w()/2, b.h()/2));
for(auto o: objs) {
assign(o);
}
objs.resize(0);
}
template<int capacity,
typename t,
typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
void quadtree<capacity, t>::assign(t o) {
rect2 orect = o.makeRect();
if(orect.intersects(nw.bounds)) {nw.addObj(o);}
if(orect.intersects(ne.bounds)) {ne.addObj(o);}
if(orect.intersects(sw.bounds)) {sw.addObj(o);}
if(orect.intersects(se.bounds)) {se.addObj(o);}
}
template<int capacity,
typename t,
typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
void quadtree<capacity, t>::empty() {
if(divided) {
divided = false;
nw.empty(); ne.empty(); sw.empty(); se.empty();
delete nw, ne, sw, se;
} else {
objs.resize(0);
}
}
根据microsoft,不完整的类型是无法确定其大小的类型,但在这里我不知道它的来源:bool divided
可以确定; quadtree<capacity, t>*
是指针,其大小可以确定; std::vector<t> objs
是一个向量,表示它存储了动态分配的数组,这意味着它的大小也可以确定; rect2 b
也只存储4个双打。知道问题可能出在哪里吗?
编辑:
这是错误消息:
bin.cpp:32:40: error: invalid use of incomplete type 'struct quadtree<capacity, t>'
32 | void quadtree<capacity, t>::addObj(t o) {
|
根据Microsoft,不完整的类型是无法确定其大小的类型