【问题标题】:Forward-declared class in boost::ptr_listboost::ptr_list 中的前向声明类
【发布时间】:2012-03-09 20:48:36
【问题描述】:

对于一个小型科学项目,我设置了一个模拟类,它将所有模拟对象保存在 ptr_list 中。因为我需要快速访问所有粒子,所以我添加了一个额外的 ptr_list。现在 boost 抱怨,因为它不喜欢前向声明的类。 recursive_wrapper 已经向我指出,但 ptr_list< recursive_wrapper<Particle> > 似乎都不起作用。

#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();
}

【问题讨论】:

    标签: c++ boost forward-declaration boost-variant boost-ptr-container


    【解决方案1】:

    我认为问题在于构造函数是由编译器隐式创建并调用 ptr_list 的构造函数。 ptr_list 构造函数使用了模板类,需要它的定义,前向声明是不够的。

    您可以通过显式声明构造函数并仅在定义模板类后定义它来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多