boost::ptr_list中正向声明的类
Forward-declared class in boost::ptr_list
对于一个小型科学项目,我设置了一个Simulation类,它将所有模拟对象保存在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构造函数使用模板化的类并需要其定义,仅向前声明是不够的。
您可以通过显式声明构造函数并仅在定义模板化类之后定义它来解决此问题。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- OpenCV - Ptr 语法和类定义/声明 - 混淆?
- 这个声明"int(*ptr[3])();"是什么意思?
- 非ptr新声明符中的错误表达式
- Boost Scoped Ptr-声明与分配
- 如何转发声明boost智能ptr
- 在声明char const *ptr ="某些字符"时是否有必要使用"const"?
- 共享Ptr与普通Ptr:声明后的对象创建