我正在用c ++编写一个程序,该程序代表具有这些类和结构的论坛:
[(ngModel)]="selectedItem"
现在,我试图将每个线程的注释的创建者放入BST中,进行排序,其中每个节点struct Date{ /* struct used to get a random day for each comment */
int d,m,y,h,min;
Date();
void GetDate() const;
};
class Post{
private:
std::string Title;
std::string Creator;
std::string text;
Date PostDate;
int id;
public:
Post();
~Post();
void postprint() const;
int idprint();
std::string get_creator() const; //returns the creator of the post
};
class Thread{
private:
std::string Subject;
std::string Creator;
Date ThreadDate;
int post_nums = rand()%10+1; // this will be the number of posts that will be made in the thread, used in threadprint()
Post *posts = new Post[post_nums]; //so the randomizer doesn't return 0 array cells, max 10 posts for each thread
public:
Thread();
~Thread();
std::string subjprint() const;
void threadprint() const;
int getnums() const; //returns the number of posts in the posts array
Post *getposts() const; //returns to a pointer the array of posts
};
class Forum{
private:
std::string Title;
Thread *threads = new Thread[3];
//void printt(int) const;
public:
Forum(const std::string);
~Forum();
void Print_threads() const;
void Print_specified_thread(const std::string);
Thread *Find_thread_subject(const std::string);
void Print_specified_post(const int);
Post *Find_specified_post(const int);
};
struct Post_list{
Post *post;
Post_list *next;
Post_list *prev;
};
struct Tree_node{
std::string Creator;
Post_list *list_ptr;
Tree_node *left;
Tree_node *right;
};
class Thread_tree{
private:
Tree_node *node_root;
void Tree_create(Tree_node *);
int Tree_empty(Tree_node *);
void Tree_sort(const Thread *, Tree_node *);
void Tree_insert(const std::string, Tree_node *);
public:
Thread_tree(const Thread *);
};
在列表中都有一个创建者字符串及其注释。{{1 }}就是这样
struct Tree_node
class Thread_tree
是
Thread_tree::Thread_tree(const Thread *Thread1){ //constructor only to be called once
cout << "Creating tree of post creators from thread " << Thread1->subjprint() << endl;
Tree_create(this->node_root); //points root to NULL
Tree_sort(Thread1, this->node_root);
}
和Tree_sort()
void Thread_tree::Tree_sort(const Thread *Thread1, Tree_node *root){
int k = Thread1->getnums(); //k is assigned to the number of the posts that the thread has
Post *post_ptr = Thread1->getposts(); //ptr is assigned to the posts array of the thread
for (int i=0; i<k; i++){
cout << "\ni is " << i << endl;
Tree_insert((post_ptr+i)->get_creator(), root);
}
}
在main()中,当我使用“ thread”作为指向现有线程的指针创建树Tree_insert()
时,遇到了分段错误。我运行gdb调试器,并在使用void Thread_tree::Tree_insert(const string creator, Tree_node *root){
if (Tree_empty(root)){
root = new Tree_node;
root->Creator = creator;
root->list_ptr = NULL;
root->left = NULL;
root->right = NULL;
}
else if (creator.compare(root->Creator) < 0){ //creator in string1 is smaller than the root->Creator
Tree_insert(creator, (root->left)); // in ascending order
}
if (creator.compare(root->Creator) > 0) {//creator in string1 is bigger than the root->Creator
Tree_insert(creator, (root->right)); //in ascending order
}
}
后收到以下消息:
Thread_tree Tree1(thread);
我不明白问题出在哪里,因为从消息中我了解到,该程序在第二次调用bt
时崩溃了
答案 0 :(得分:0)
您的问题是Tree_create(this->node_root);
Tree_create
创建传递给它的指针的副本,并将NULL
分配给该副本,而this->node_root
保持不变,并且在调用Tree_sort
时未初始化。
最简单的解决方案是将NULL
(或nullptr
中最好的c++11
)直接分配给this->node_root
其他方法也一样。当按值传递指针时,对其的任何赋值都将在复制时完成。解决方案是通过引用传递指针(例如Tree_create(Tree_node*& root)
)