递归破坏者和类型
我正在编程一个搜索树,该搜索树由两种类型组成: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在类外部定义类方法(包括构造函数和驱动器),则每次将标题包含在内时,都会定义该方法。
有2个解决方案:
将该方法放入CPP文件中,因此仅收集一次。
将方法标记为
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:
Put the method in a cpp file so it only gets compiled once.
Mark the method as
inline
so all versions of the method collapse into a single instance when linking.