gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:33:03 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com