简体   繁体   中英

Forward-declared class in boost::ptr_list

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM