使用enable_shared_from_this的谷物库错误

纳坎普

编辑:创建了一个最小的VS解决方案,以使其更容易重现该错误:https : //www.dropbox.com/s/pk0t8t2xykjmtc5/test%20cereal%20this.zip(在其中包含谷类而不是$(LIBSROOT)中,有它)。

我收到2个错误,指出我没有默认的构造函数:

error C2139: 'Node' : an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_constructible'

error C2338: Trying to serialize a an object with no default constructor.
<path>\cereal\details\traits.hpp line 1248

但是我认为类的默认构造函数应该可以。如果我注释掉Node类变量的序列化,我会得到相同的错误,但是零件类变量却存在。

我具有以下代码结构(省略了一些部分,例如包括保护或不相关的代码,如果需要,我当然可以提供全部内容,但我想使其尽可能短):

Shape.h:

#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
#include <string>

class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;

class Shape {
private:
    std::vector<PartPtr> parts;
    NodePtr root;
    std::vector<std::vector<NodePtr>> levels;


public:
    Shape();
    Shape(std::string fileName);

    template <class Archive>
    void serialize(Archive & ar) {
        ar(parts, levels, root);
    }
};

Shape.cpp:

#include "Shape.h"
#include "Node.h"
#include "Part.h"

Shape::Shape() {
}

Shape::Shape(std::string fileName) {
    // omitted code
}

Node.h:

#include "PointCloud.h"

#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>

class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;

class Node : public std::enable_shared_from_this<Node> {
private:
    std::vector<PartPtr> parts;
    NodePtr parent;
    PointCloud pointCloud;

public:
    Node();
    Node(std::vector<PartPtr> parts, NodePtr parent);

    template <class Archive>
    void serialize(Archive & ar) {
        ar(parts, parent, pointCloud);
    }
};

Node.cpp:

#include "Node.h"
#include "Part.h"

Node::Node() {
}

Node::Node(std::vector<PartPtr> parts, NodePtr parent) : parts(parts), parent(parent) {
    // code omitted
}

Part.h:

#include "PointCloud.h"
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>

class Contact;
class Part;
typedef std::shared_ptr<Contact> ContactPtr;
typedef std::shared_ptr<Part> PartPtr;

class Part : public std::enable_shared_from_this<Part> {
private:
    PointCloud pointCloud;
    std::vector<ContactPtr> contacts;
public:
    Part();
    Part(double diameter);

    template <class Archive>
    void serialize(Archive & ar) {
        ar(pointCloud, contacts);
    }
};

Part.cpp:

#include "Part.h"
#include "Contact.h"

Part::Part() {
}

Part::Part(double diameter) {
    // omitted code
}

Contact类包含一个PartPtr作为成员变量,PointCloud仅包含一堆Eigen :: Matrix数据(可能也应该是一个智能指针,以加快代码的速度,但这对这个问题并不重要)。

有什么建议如何解决这个问题?还是可能是一个错误?我正在使用VS2013,这也可能是原因。

maddin45

您只class Part;class Node;声明Shape.h要将类用作模板参数,编译器还需要该类的定义。文件中包含Part.h还包括inin 并在头文件中放置一些include防护:Node.hShape.hPart.hNode.hContact.hPart.h

#ifndef SOME_UNIQUE_IDENTIFIER
#define SOME_UNIQUE_IDENTIFIER

// content of your header goes here

#endif

通常,SOME_UNIQUE_IDENTIFIER在Node.h文件中为NODE_H,在Part.h文件中为PART_H。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在模板函数中使用enable_shared_from_this?

从std :: enable_shared_from_this继承

std :: enable_shared_from_this; 公共与私人

OS X上std :: enable_shared_from_this <>的编译错误

std :: enable_shared_from_this :: shared_from_this如何工作

我们什么时候应该使用std :: enable_shared_from_this

为什么我们使用 enable_shared_from_this 模板?

为什么 std::enable_shared_from_this 不使用可变的 std::weak_ptr ?

如何在父类和子类中使用std :: enable_shared_from_this?

可以在不继承的情况下使用enable_shared_from_this吗?

在具有虚拟析构函数的多态继承中使用 enable_shared_from_this

“ enable_shared_from_this”的作用是什么?

Qt QObject和boost :: enable_shared_from_this

enable_shared_from_this 中的空弱指针

如何从boost :: enable_shared_from_this派生模板模板类?

从 enable_shared_from_this 继承的目的是什么?

尝试了解std :: enable_shared_from_this <T>,但使用它会导致bad_weak_ptr

pybind11,在使用 std::enable_shared_from_this 绑定 Trampolines 和多重继承时编译失败

std :: enable_shared_from_this:是否可以在析构函数中调用shared_from_this()?

检查从enable_shared_from_this派生的对象是否由shared_ptr管理?

从enable_shared_from_this继承并返回self的shared_ptr的类的子类

为什么std :: enable_shared_from_this允许多个std :: shared_ptr实例?

make_shared不能与enable_shared_from_this一起玩吗?

enable_shared_from_this和make_shared是否提供相同的优化

为继承enable_shared_from_this的类获取unique_ptr

必须enable_shared_from_this是第一个基类?

enable_shared_from_this <S>什么时候有用?

可以从std :: enable_shared_from_this和抽象基类派生吗?

std :: enable_shared_from_this与其他所有者