gpt4 book ai didi

c++ - 获取类型为 'Node' 的临时对象的地址

转载 作者:行者123 更新时间:2023-11-30 05:36:47 25 4
gpt4 key购买 nike

t.PreorderTraversal(t, &t.getRoot()); 错误是获取“节点”类型的临时对象的地址。 Root 是一个 Node 类对象。函数PreoderTraversal 将Node 对象作为一个点,所以我给出了Node 对象的地址和错误发生的地方。这不是正确的做法吗?

class NodeList;

class Node {
private:
Node* parent;
int elem;
NodeList* children;
Node *next;
Node *prev;

};

class NodeList {
public:
NodeList();
void addNodeAtRank(int, int);
private:
Node* header;
Node* tailer;
};

class Tree {
private:
int n;
Node root;
public:
Tree();
void addNode(Tree &t, int, int, int);
void PreorderTraversal(const Tree& t, Node* p);
void PostorderTraversal(const Tree& t, Node* p);
void printXYofNode(const Tree& t, int nodeNumber);
Node getRoot();
Node* getNodeByElem(Node& n, int);
};

Node::Node() {

children = nullptr;
parent = nullptr;
elem = 0;
next = nullptr;
prev = nullptr;

}

NodeList::NodeList() {

header = new Node();
tailer = new Node();

header->next = tailer;
tailer->prev = header;
}

void NodeList::addNodeAtRank(int rank, int e) {

Node *v = new Node();
v->elem = e;

int count = 1;
Node *NodeAtRank = header->next;

while (count != rank) {
NodeAtRank = NodeAtRank->next;
count++;
}

v->next = NodeAtRank;
v->prev = NodeAtRank->prev;
NodeAtRank->prev = v;
NodeAtRank->prev->next = v;

}

bool NodeList::empty() const {
return header->next == tailer;
}

Tree::Tree() {

n = 0;
//root = Node();
}

void Tree::addNode(Tree& t, int NodeElement, int ParentNode, int SiblingOrder) {

//Node *treeNode = new Node();

if (t.empty() && ParentNode == -1 && SiblingOrder == -1) {
t.root = Node();
t.root.elem = NodeElement;
t.root.children = new NodeList();
} else {

Node* nodeParent = t.getNodeByElem(t.root, ParentNode);

NodeList *childrenNodelist = nodeParent->children;
childrenNodelist->addNodeAtRank(SiblingOrder, NodeElement);

nodeParent->children = childrenNodelist;
}

n++;
}

Node* Tree::getNodeByElem(Node& root, int nodeElem) {

if (root.elem == nodeElem)
return &root;
else {
NodeList *rootChildren = root.children;

Node *head = rootChildren->header;

while (head->next != rootChildren->tailer) {

if (!head->next->isExternal())
return getNodeByElem(*(head->next), nodeElem);
else {
if (head->next->elem == nodeElem)
return head->next;

head = head->next;
}
}

return new Node();
}
}

void Tree::PreorderTraversal(const Tree& t, Node* p) {

cout << p->elem;
NodeList *mychildren = p->children;
Node *traversal = mychildren->header->next;

while (traversal != mychildren->tailer) {
cout << " ";
PreorderTraversal(t, traversal->next);
traversal = traversal->next;
}

}

void Tree::PostorderTraversal(const Tree& t, Node* p) {

NodeList *mychildren = p->children;
Node *traversal = mychildren->header->next;

while (traversal != mychildren->tailer) {
PreorderTraversal(t, traversal);
traversal = traversal->next;
}
cout << p->elem;
}

bool Tree::empty() const {
return n == 0;
}

int Tree::size() const {
return n;
}

Node Tree::getRoot() {
return root;
}

int main(int argc, const char * argv[]) {

char Type = NULL;
int nodeNumber = 0;
int nodeParent = 0;
int nodeOrderInSibling = 0;

Tree t = Tree();
cin >> Type;
while (Type != 'Q') {
if (Type == 'I') {
cin >> nodeNumber >> nodeParent >> nodeOrderInSibling;
t.addNode(t, nodeNumber, nodeParent, nodeOrderInSibling);
} else if (Type == 'P') {
t.PreorderTraversal(t, &t.getRoot());
} else if (Type == 'T') {
t.PostorderTraversal(t, &t.getRoot());
} else if (Type == 'C') {
cin >> nodeNumber;
t.printXYofNode(t, nodeNumber);
} else {
cout << "Wrong input type!!!" << endl;
}

cin >> Type;
}

return 0;
}

最佳答案

此函数返回 Node 对象的拷贝,该对象是 Tree 的成员

Node getRoot();

所以在这一行中,您将获得此对象的地址,该对象随后将被丢弃。

t.PreorderTraversal(t, &t.getRoot());

您留下的指针称为悬垂指针,因为它没有指向有效对象。

考虑像这样修改getRoot

Node* Tree::getRoot() {
return &root;
}

当然,您必须确保在使用此指针时 Root 对象不会超出范围

关于c++ - 获取类型为 'Node' 的临时对象的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com