递归破坏者和类型

发布于 2025-02-03 18:22:28 字数 1526 浏览 3 评论 0原文

我正在编程一个搜索树,该搜索树由两种类型组成:matrixnode和chancenode。 MatrixNode和Chancenode都有以下三个成员:父母,下一个和孩子。

但是,树的结构使两种类型的节点将交替。矩阵诺德的孩子是一个chancenode,依此类推。 “下一个”总是相同的类型。

我试图为这些课程定义攻击函数。我想在这一点上做一个递归驱动器,因为我不认为递归会导致堆栈溢出,而且我还不知道如何通过其他方式做到这一点。

在定义这些类时,由于类型的“循环”性质,我必须使用foward声明进行全面的声明。我尝试了灾难类似的东西:

class ChanceNode;

class MatrixNode {
public:
    ChanceNode* parent = nullptr;
    ChanceNode* child = nullptr;
    MatrixNode* next = nullptr;

    ~MatrixNode ();
};

class ChanceNode {
public:
    MatrixNode* parent = nullptr;
    MatrixNode* child = nullptr;
    ChanceNode* next = nullptr;

    ~ChanceNode () {
        delete child;
        delete next;
    };
};

MatrixNode :: ~MatrixNode() {
    delete child;
    delete next;
}

但是这不起作用:

build] /usr/bin/ld: CMakeFiles/surskit.dir/src/main.cc.o: in function `State::actions(PairActions)':
[build] /home/user/Desktop/surskit/src/tree/node.hh:59: multiple definition of `MatrixNode::~MatrixNode()'; CMakeFiles/surskit.dir/src/tree/node.cc.o:/home/user/Desktop/surskit/src/tree/node.hh:59: first defined here
[build] /usr/bin/ld: CMakeFiles/surskit.dir/src/main.cc.o: in function `State::actions(PairActions)':
[build] /home/user/Desktop/surskit/src/tree/node.hh:59: multiple definition of `MatrixNode::~MatrixNode()'; CMakeFiles/surskit.dir/src/tree/node.cc.o:/home/user/Desktop/surskit/src/tree/node.hh:59: first defined here

在这种情况下,是否有可能具有递归破坏者?顺便说一句,我尝试声明一个基本节点类,并从此派生矩阵和机会节点。这样,我可以在基类中定义递归破坏者。但这似乎更不可行。

I am programming a search tree that will consist of two types: MatrixNode and ChanceNode.
Both MatrixNode and ChanceNode have the following three members: parent, next, and child.

However, the structure of the tree is such that the two types of node will alternate. The child of a MatrixNode is a ChanceNode and so on. "Next" is always the same type.

I am trying to define a Destructor for these classes. I want to do a recursive destructor at this point since I don't believe the recursion will cause a stack overflow, and I am not knowing just yet how to do it via other means.

When defining these classes I had to use a foward declaration for ChanceNode due to the 'cyclic' nature of the types. I tried something similar with the destructors:

class ChanceNode;

class MatrixNode {
public:
    ChanceNode* parent = nullptr;
    ChanceNode* child = nullptr;
    MatrixNode* next = nullptr;

    ~MatrixNode ();
};

class ChanceNode {
public:
    MatrixNode* parent = nullptr;
    MatrixNode* child = nullptr;
    ChanceNode* next = nullptr;

    ~ChanceNode () {
        delete child;
        delete next;
    };
};

MatrixNode :: ~MatrixNode() {
    delete child;
    delete next;
}

However this does not work:

build] /usr/bin/ld: CMakeFiles/surskit.dir/src/main.cc.o: in function `State::actions(PairActions)':
[build] /home/user/Desktop/surskit/src/tree/node.hh:59: multiple definition of `MatrixNode::~MatrixNode()'; CMakeFiles/surskit.dir/src/tree/node.cc.o:/home/user/Desktop/surskit/src/tree/node.hh:59: first defined here
[build] /usr/bin/ld: CMakeFiles/surskit.dir/src/main.cc.o: in function `State::actions(PairActions)':
[build] /home/user/Desktop/surskit/src/tree/node.hh:59: multiple definition of `MatrixNode::~MatrixNode()'; CMakeFiles/surskit.dir/src/tree/node.cc.o:/home/user/Desktop/surskit/src/tree/node.hh:59: first defined here

Is it possible to have a recursive destructor in this case? By the way, I tried declaring a base Node class and deriving Matrix and Chance node from this. This way, I could define the recursive destructor in the base class. But this seems even less viable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宁愿没拥抱 2025-02-10 18:22:28

如果您在类外部定义类方法(包括构造函数和驱动器),则每次将标题包含在内时,都会定义该方法。

有2个解决方案:

  1. 将该方法放入CPP文件中,因此仅收集一次。

  2. 将方法标记为inline,因此该方法的所有版本在链接时崩溃为单个实例。

If you define a class method (including constructors and destructors) outside the class then the will be defined every time the header is included leading to multiple definitions.

There are 2 solutions to this:

  1. Put the method in a cpp file so it only gets compiled once.

  2. Mark the method as inline so all versions of the method collapse into a single instance when linking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文