0

I'm trying to implement a quadtree, and I want to put it in a library so that I don't have to copy and refactor the code every time I need it. What I've came up with are a hasPos and a hasDim interfaces so that I have rectangle-like objects to work with:

class hasPos {
protected:
  vec2 pos;
};

class hasDim: public hasPos {
protected:
  vec2 dim;
  // ...
};

template<int capacity, typename t, typename = std::enable_if<std::is_base_of<hasDim, t>::value && std::is_pointer<t>::value>>
class quadtree {
  quadtree<capacity, t> nw, ne, sw, se; // for subdivision when capacity is exceeded
  // ...
};

When I compile the library it works just fine, but when I compile a project with a quadtree in it, the compiler complains about an incomplete type:

error: 'quadtree<capacity, t, <template-parameter-1-3> >::ne' has incomplete type

It really looks as if std::enable_if is the problem.

EDIT :

As suggested in the comments, I turned nw, ne, sw, se into *nw, *ne, *sw, *se. It seemed to work, but a few hours later it's again complaining about an incomplete type, after I moved the implementation into a compilation unit:

bin.cpp:32:40: error: invalid use of incomplete type 'struct quadtree<capacity, t>'
   32 |  void quadtree<capacity, t>::addObj(t o) {
      |
lenerdv
  • 177
  • 13
  • 1
    The problem is that a `quadtree` contains a `quadtree` that contains another `quadtree` that contains yet another `quadtree` and so on. The error stems from the fact that while parsing the class `quadtree`, before it's definition is complete, you are trying to use it by value. Since the definition is not yet complete (the compiler does not know the size of this type for example) it's incomplete and can't be used by value. You could use pointers though. – super Apr 04 '20 at 09:17
  • yes, thanks to both @super and – lenerdv Apr 04 '20 at 09:20
  • @L.F. ......... – lenerdv Apr 04 '20 at 09:20

0 Answers0