C++ 递归函数给出分段错误,如何将指针对象指向它';谁的孩子?
所以我有一个prefixtree对象,它有多个节点。每个节点都由一个字符组成,不管它是最终节点还是对象指针数组中存储的子节点(最多26个值)。我需要打印在给定节点下找到的单词 下面的例子C++ 递归函数给出分段错误,如何将指针对象指向它';谁的孩子?,c++,nodes,prefix-tree,C++,Nodes,Prefix Tree,所以我有一个prefixtree对象,它有多个节点。每个节点都由一个字符组成,不管它是最终节点还是对象指针数组中存储的子节点(最多26个值)。我需要打印在给定节点下找到的单词 下面的例子 a / \ b c \ t 如果在具有字符“a”的节点上调用该函数,则应打印ab并执行操作。我计划通过添加到字符串中,直到到达标记为final的节点,然后删除该字母来实现这一点。我想实现一个递归函数,但是当将一个节点设置为该节点的子节点时,我得到了一个分段错误 void PrefixTr
a
/ \
b c
\
t
如果在具有字符“a”的节点上调用该函数,则应打印ab并执行操作。我计划通过添加到字符串中,直到到达标记为final的节点,然后删除该字母来实现这一点。我想实现一个递归函数,但是当将一个节点设置为该节点的子节点时,我得到了一个分段错误
void PrefixTreeNode::printAllWords() const
{
PrefixTreeNode* node;
for(char i = 'a'; i < 'a' + ALPHABET_SIZE; i++)
{
if(getChild(i) != nullptr)
{
if(!isFinal())
{
nodeList.push_back(i);
cout << "added: " << i << endl;
node = node->getChild(i); //this line results to segmentation fault
node->printAllWords(); //How would I call the function on the node's child?
}
else if(isFinal())
{
nodeList.push_back(i);
cout << nodeList;
nodeList.pop_back();
return;
}
}
}
}
节点对象:
class PrefixTreeNode
{
friend PrefixTree;
private:
char c;
bool final;
PrefixTreeNode* link[ALPHABET_SIZE];
public:
//Constructs a new node
PrefixTreeNode();
//Copy constructor
PrefixTreeNode(const PrefixTreeNode&);
//Copy assignment
const PrefixTreeNode& operator=(const PrefixTreeNode&);
//Returns the character this node contains
char getChar() const { return c; }
//Returns whether this node is the end of a word
bool isFinal() const { return final; }
//Changes whether this node is the end of a word
void setFinal(bool b) { final = b; }
//Returns the node corresponding to the given character
PrefixTreeNode* getChild(char);
//Returns the node corresponding to the given character
const PrefixTreeNode* getChild(char) const;
//Adds a child corresponding to the given character
void addChild(char);
//Removes the child corresponding to the given character
void deleteChild(char);
//TODO: print all words that end at or below this PrefixTreeNode
void printAllWords() const;
//Destructor
~PrefixTreeNode();
};
如果我只是告诉你为什么这条线是由段故障引起的,我可以说你可以初始化这条线上面的变量节点 我不知道整个代码,但我建议将其更改为这样
void PrefixTreeNode::printAllWords() const
{
for(char i = 'a'; i < 'a' + ALPHABET_SIZE; i++)
{
PrefixTreeNode* node = getChild(i);
//if(getChild(i) != nullptr)
if(node != nullptr)
{
nodeList.push_back(i);//this line will be run, ALWAYS whatever the return of isFinal, so I moved it to here.
if(!isFinal())
{
//nodeList.push_back(i); //moved to the front of 'if'
cout << "added: " << i << endl;
//just remove it
//node = node->getChild(i); //this line results to segmentation fault
node->printAllWords(); //How would I call the function on the node's child?
}
//else if(isFinal()) //duplicated
else
{
//nodeList.push_back(i); //moved to the front of 'if'
cout << nodeList; //I don't know the type of nodeList, but you may take care of it.
nodeList.pop_back();
return;
}
}
}
}
void PrefixTreeNode::printalwords()常量
{
for(字符i='a';i<'a'+字母表大小;i++)
{
PrefixTreeNode*node=getChild(i);
//if(getChild(i)!=nullptr)
如果(节点!=nullptr)
{
nodeList.push_back(i);//无论isFinal返回什么,这一行都会运行,所以我把它移到了这里。
如果(!isFinal())
{
//nodeList.push_back(i);//移到“if”的前面
你能分享一个吗?我应该可以运行你的代码并看到问题所在。谢谢。当你调用node->getChild(I)时,你的节点没有初始化。但是您的示例代码不完整,我们不知道getChild做什么。PrefixTreeNode*node;
未初始化。它指向单词之间的空间,这是一个惊人的未定义行为的领域。node=node->getChild(i)
看起来它进入了这个神秘的仙境,寻找子节点。这种探索很少有好的结局。@ggorlen,因为这涉及到具有构造函数、函数和运算符重载的对象,我认为什么是最小的?@Nico238请告诉我添加的部分是否有用。啊..解释错误的注释是在我写这个答案的时候,dy发布了…嗯:(我很感激,伙计,你花了很多时间写出来。如果我请求你帮助解决它背后的逻辑,这很酷吗?nodeList是一个字符串,我只是想列出所有的单词。所以abc、abcd和abcdef都添加了单词,但我在删除正确的字符时遇到了问题。也许你能帮我解决吗?我已经解决了我真的很感谢你花时间帮我兄弟
void PrefixTreeNode::printAllWords() const
{
for(char i = 'a'; i < 'a' + ALPHABET_SIZE; i++)
{
PrefixTreeNode* node = getChild(i);
//if(getChild(i) != nullptr)
if(node != nullptr)
{
nodeList.push_back(i);//this line will be run, ALWAYS whatever the return of isFinal, so I moved it to here.
if(!isFinal())
{
//nodeList.push_back(i); //moved to the front of 'if'
cout << "added: " << i << endl;
//just remove it
//node = node->getChild(i); //this line results to segmentation fault
node->printAllWords(); //How would I call the function on the node's child?
}
//else if(isFinal()) //duplicated
else
{
//nodeList.push_back(i); //moved to the front of 'if'
cout << nodeList; //I don't know the type of nodeList, but you may take care of it.
nodeList.pop_back();
return;
}
}
}
}