for a small science project I set up a Simulation class which holds all simulated Objects in a ptr_list. Because I need to have fast access to all Particles I added an additional ptr_list. Now boost complains, because it doesn't like forward declarated classes. recursive_wrapper
was already pointed out to me, but ptr_list< recursive_wrapper<Particle> >
seems to work neither.
#include <boost/ptr_container/ptr_list.hpp>
class SimulatedObject {
};
class Particle; // derived from SimulatedObject
class Simulation {
public:
void addObj(SimulatedObject *obj) {
simulatedObjects.push_back(obj);
}
void addObj(Particle *par) {
particles.push_back(par);
}
protected:
boost::ptr_list<SimulatedObject> simulatedObjects;
boost::ptr_list<Particle> particles;
};
int main(int argc, char** argv) {
Simulation sim();
}
I think the problem is the constructor is implicitly created by the compiler and calls the constructor of the ptr_list. The ptr_list constructor uses the templated class and needs its definition, the forward declaration is not enough.
You can fix this by declaring the constructor explicitly and defining it only after the templated class is defined.