简体   繁体   English

在 std::__cxx11::basic_string 中获取未知的分段错误“(地址) <char, std::char_traits<char> ,..., std::allocator<char> &gt; 常量&amp;) 常量()

[英]Getting unknown Segmentation Fault "(address) in std::__cxx11::basic_string<char, std::char_traits<char>,..., std::allocator<char> > const&) const ()

I'm making a program in c++ that represents a forum which has those classes and structs:我正在用 C++ 编写一个程序,它代表一个具有这些类和结构的论坛:

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 *);
};

Now I am trying to put the creators of the comments of each Thread into a BST, sorting them out, where each node struct Tree_node has a creator string and his comments on a list.The constructor of class Thread_tree is something like this现在我试图将每个线程的评论的创建者放入一个 BST 中,将它们整理出来,其中每个节点struct Tree_node都有一个创建者字符串和他在列表中的评论。 class Thread_tree构造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);
}

while Tree_sort() isTree_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);
  }
}

and Tree_insert()Tree_insert()

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
      }
}

In main() when I make a tree Thread_tree Tree1(thread);在 main() 中,当我制作一棵树Thread_tree Tree1(thread); with 'thread' being a pointer pointing to an existing thread, I get a segmentation fault.由于“线程”是指向现有线程的指针,因此出现分段错误。 I run the gdb debugger and after using bt I get this message:我运行 gdb 调试器,并在使用bt后收到此消息:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b7625a in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b7625a in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00000000004025f1 in Thread_tree::Tree_insert (this=0x7fffffffe7c0, creator="user1234", root=0x1002) at classes.cpp:211
#2  0x00000000004026be in Thread_tree::Tree_insert (this=0x7fffffffe7c0, creator="user1234", root=0x604110) at classes.cpp:218
#3  0x00000000004024e4 in Thread_tree::Tree_sort (this=0x7fffffffe7c0, Thread1=0x616d98, root=0x604110) at classes.cpp:197
#4  0x00000000004023b7 in Thread_tree::Thread_tree (this=0x7fffffffe7c0, Thread1=0x616d98) at classes.cpp:181
#5  0x0000000000402f03 in main () at main.cpp:19

I can't understand what the problem is, as from what I understand from the message, the program crushes at the second call of Tree_insert()我不明白问题是什么,从我从消息中了解到的,程序在Tree_insert()的第二次调用时Tree_insert()

Your problem is Tree_create(this->node_root);你的问题是Tree_create(this->node_root);

Tree_create creates copy of pointer passed to it and assigns NULL to this copy, while this->node_root stays unchanged and is uninitialized when Tree_sort is called. Tree_create创建传递给它的指针的副本并将NULL分配给该副本,而this->node_root保持不变并且在Tree_sort时未初始化。

Simplest solution is to assign NULL (or nullptr which is preferable in c++11 ) directly to this->node_root最简单的解决方案是将NULL (或nullptr ,在c++11更可取)直接分配给this->node_root

The same applies to your other methods.这同样适用于您的其他方法。 When you pass pointer by value, any assignment to it is done on copy.当您按值传递指针时,对它的任何赋值都是在复制时完成的。 Solution here is to pass pointers by reference (eg Tree_create(Tree_node*& root) )这里的解决方案是通过引用传递指针(例如Tree_create(Tree_node*& root)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义对`readVector(std :: __ cxx11 :: basic_string的引用) <char, std::char_traits<char> ,std :: allocator <char> &gt;)” - undefined reference to `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' “ str2Int(std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt; const&)”,引用自: - “str2Int(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)”, referenced from: ostream和运算符std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt;? - ostream and operator std::basic_string<char, std::char_traits<char>, std::allocator<char>>? 'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char> - no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'}) 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 获取“错误:无法转换 'std::__cxx11::string* {aka std::__cxx11::basic_string<char> *}' 到 'const char*' 错误,同时将路径作为参数传递</char> - Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? main.cpp || 未定义对`MyClass :: loadDatas(std :: basic_string的引用 <char, std::char_traits<char> ,std :: allocator <char> &gt;)&#39;| - main.cpp|| undefined reference to `MyClass::loadDatas(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'| 未定义对`librarymanager :: getBook(std :: basic_string的引用 <char, std::char_traits<char> ,std :: allocator <char> &gt;) - undefined reference to `librarymanager::getBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >) 1 IntelliSense:没有合适的构造函数可以从“bool”转换为“std :: basic_string” <char, std::char_traits<char> ,std :: allocator <char> &gt;” - 1 IntelliSense: no suitable constructor exists to convert from “bool” to “std::basic_string<char, std::char_traits<char>, std::allocator<char>>”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM