【发布时间】:2018-10-12 16:18:49
【问题描述】:
对于一个练习,我想打印一个基于 Node 对象的树数据结构。这意味着,每个对象都有一个向量节点,该节点再次保存其他类型为 Node 的对象。但是由于某种原因,当我打印出叶节点的 this->get_nr_children 时,它基本上只返回 nodes.size(),我得到完全随机(负)整数,它实际上应该返回 0。更有趣的部分:每个当我编译和执行时,它会打印出不同的整数,这些整数总是一些低负数。我不知道发生了什么!
节点.h
#include <string>
#include <vector>
using namespace std;
class Node
{
public:
virtual ~Node();
Node(string name = "");
string get_name() const;
void set_name(string& new_name);
int get_nr_children() const;
Node* get_child(int i) const;
void add_child(Node child);
void create_complete_tree(int nr_child_nodes, int tree_depth);
void print();
private:
string name;
static int node_id;
vector<Node> nodes = {};
};
节点.cpp
#include "node.h"
#include <sstream>
using namespace std;
Node::Node(string name) {
node_id++;
nodes = {};
if (name == "") {
stringstream str_sm;
str_sm << (node_id);
string node_id_str = str_sm.str();
this->name = "node_" + node_id_str;
} else {
this->name = name;
}
}
Node::~Node() {
nodes.clear();
// node_id = 0;
}
int Node::node_id = 0;
string Node::get_name() const {
return name;
}
void Node::set_name(string& new_name) {
this->name = new_name;
}
int Node::get_nr_children() const {
return nodes.size();
}
Node* Node::get_child(int i) const {
if (i >= nodes.size()) {
return NULL;
}
Node node = nodes[i];
Node *ptrNode = &node;
return ptrNode;
}
void Node::add_child(Node child) {
nodes.push_back(child);
}
void Node::create_complete_tree(int nr_child_nodes, int tree_depth) {
tree_depth--;
if (tree_depth <= 0) {
return;
}
for (int i = 0; i < nr_child_nodes; i++) {
Node* node = new Node();
this->add_child(*node);
node->create_complete_tree(nr_child_nodes, tree_depth);
}
}
void Node::print() {
cout << this->get_name() << "\n";
cout << "I got this many children " << this->get_nr_children();
for (int i = 0; i < this->get_nr_children(); i++) {
cout << "\t";
this->get_child(i)->print();
cout << "\n";
}
}
main.cpp
#include <iostream>
#include "node.cpp"
using namespace std;
int main() {
Node* root = new Node("root");
Node* left_child = new Node("left child");
Node* right_child = new Node("right child");
root->add_child(*left_child);
root->add_child(*right_child);
root->print();
return 0;
}
当我执行它时,我得到:
root 我有这么多孩子 2 left child 我有这么多孩子 -62802357 对的孩子 我有这么多孩子 -62802357
进程以退出代码 0 结束
【问题讨论】:
-
您的问题很可能是
get_nr_children返回int,因此nodes.size()从size_t转换为int,然后溢出。 -
这不会回答您的问题,但您应该知道
add_child(*node);将*node的副本 放在子向量中。对note的进一步更改(例如在create_complete_tree中)将不会反映在您添加的副本中。
标签: c++ pointers recursion tree cout