c++ - boost::ptr_list 中的前向声明类

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

对于一个小型科学项目,我设置了一个模拟类,它将所有模拟对象保存在 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();
}

最佳答案

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

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

关于c++ - boost::ptr_list 中的前向声明类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9640794/

相关文章:

c++ - 从 C++ 调用 Visual Basic 函数

c++ - 将对象加载到指向对象的指针数组中

c++ - 使用宏计算源文件行数?

c++ - 处理文件名boost::filesystem::stem中带有多个“。”的文件

c++ - 从共享库导出模板化 C++ 类在 Linux 上如何工作?

c++ - 仅在库 cpp 中具有定义的前向声明结构

c++ - 使用 boost::locale/ICU 边界分析与中文

c++ - 只计算递增次数的输出迭代器?

c++ - 不完整类型/前向声明的无效使用

c++ - 如何从类定义中省略私有(private)非虚拟方法?