C++ 分段故障C++;

C++ 分段故障C++;,c++,segmentation-fault,C++,Segmentation Fault,我不知道为什么我的代码是seg错误,我假设我看到的是队列中一个没有任何东西的点,但我认为我确实将元素推到了队列中 这是我的密码: template <typename T> class btree { public: btree(size_t maxNodeElems); ~btree() {} struct node { list <T> elements; node *lvl; }; priv

我不知道为什么我的代码是seg错误,我假设我看到的是队列中一个没有任何东西的点,但我认为我确实将元素推到了队列中

这是我的密码:

template <typename T> 
class btree {

  public:
    btree(size_t maxNodeElems);
    ~btree() {}

    struct node {  
      list <T> elements;
      node *lvl;
    };

  private:
    size_t maxNodeElems;
    node*  root;   

};

template <typename T>
btree<T>::btree(size_t maxNodeElems) {
  if (maxNodeElems > 0) max = maxNodeElems;
  root = new node;
  root->lvl = new node*[max+1];
  for (int i = 0; i < (int) max+1; i++) root->lvl[i] = new node;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {

  queue <typename btree<T>::node*> q;
  q.push(tree.root);
  int loop = 0;
  while (!q.empty()) {
    loop++;
    typename btree<T>::node* temp = q.front();
    int i = 0;

    class list <T>::iterator itr = temp->elements.begin();
    for (; itr != temp->elements.end(); ++itr) {
      os << *itr << " ";
      if (!temp->lvl[i]->elements.empty()) {
        q.push(temp->lvl[i]);
      }
      i++;
    }

    q.pop();
  }

  return os;
}
在my test.cpp中:

int main (void) {

  btree<char> b(2);
  b.insert('Z');   b.insert('J');   b.insert('Y');
  cout << b; 
  return 0;
}
int main(无效){
b树b(2);
b、 插入('Z');b.插入('J');b.插入('Y');

您发布的代码无法编译,因此我无法验证您的错误

下面的代码确实可以编译,因此可能会提供一些帮助

但是,下面的代码不太好——例如,在构造函数中使用new关键字时需要小心,因为在构造函数中处理异常更加困难(并且有可能引发分配异常)

模板
B类树{
公众:
b树(最大节点数);
//如果您正在使用new,则需要删除-在析构函数中执行该操作。
~b树();
结构节点{
列出要素;
//将其用作指向数组的指针,
//它可以降级为双指针-
//但不是像你那样的一个指针。
节点**lvl;
};
私人:
size\u t maxNodeElems;//您实际上没有在代码中使用此变量
节点*根;
//您正在构造函数中进行内存分配,
//因此需要重载运算符=并复制构造函数。
//因为我们没有重载这些,所以我们将它们设置为私有。
void运算符=(常量btree&);
b树(常数b树&);
};
//在代码中有一个未声明的变量max。
//不知道它是什么,也不知道你为什么把它拿走。
模板
btree::btree(大小v最大节点数)
:maxNodeElems(v_maxNodeElems)//构造定义的变量
{  
根=新节点;
root->lvl=newnode*[maxNodeElems+1];//root->lvl是一个双指针
对于(int i=0;i<(int)maxNodeElems+1;i++)
root->lvl[i]=新节点;
}
//按与您相反的顺序删除
模板
btree::~btree(){
对于(int i=0;i<(int)maxNodeElems+1;i++)
删除根->lvl[i];//首先删除数组的元素
delete[]root->lvl;//然后是数组-注意delete[]
删除根;//然后是根。
}
int
主(内部交流,字符**av)
{
b树b(2);
}

用调试符号(
gcc-g-O0
)编译并通过
valgrind
@BrendanLong运行它做什么(我对编程还是很陌生),它会告诉你代码在哪里崩溃。这些指令在Linux上可以运行(它会准确地告诉你程序崩溃的位置和原因)。通过使用IDE的调试器,您可以在其他平台上获得类似的结果。@BrendanLong所以只需以
gcc-g-O0-Wall-Werror-o test test.cpp
或类似的方式运行它?是的,然后运行
valgrind test
(在名为“test”的可执行文件上运行valgrind)。可能任何学校的计算机都会有它。如果是家用计算机,请执行任何命令安装软件包来安装它(
aptitude install valgrind
yum install valgrind
pacman-s valgrind
int main (void) {

  btree<char> b(2);
  b.insert('Z');   b.insert('J');   b.insert('Y');
  cout << b; 
  return 0;
}
template <typename T> 
class btree {

public:
  btree(size_t maxNodeElems);
  //if your using new then you need to delete - do that in the destructor.
  ~btree();
  struct node {  
    list <T> elements;
      //you use this as a pointer to an array, 
      //  it can be demoted to a double pointer - 
      //  but not a single pointer as you had.
      node ** lvl;
    };

  private:
    size_t maxNodeElems;  //you don't actually use this variable in your code
    node*  root;   
   //You are doing memory allocations in the constructor, 
   //  therefore need to overload operator= and copy constructor. 
   //  Since we are not overloading these, we make them private.
   void operator=(const btree&);
   btree(const btree&);

};


//In your code you had a undeclared variable max. 
// No idea what it was or why you had it - removed.
template <typename T>
btree<T>::btree(size_t v_maxNodeElems) 
  : maxNodeElems(v_maxNodeElems)  // construct the variable you define
{  
  root = new node;
  root->lvl = new node*[maxNodeElems+1];  //root->lvl is a double pointer
  for (int i = 0; i < (int) maxNodeElems+1; i++) 
    root->lvl[i] = new node;
}

//delete in reverse order to you constructing
template <typename T>
btree<T>::~btree() {
  for (int i = 0; i < (int) maxNodeElems+1; i++) 
    delete root->lvl[i];   //first the elements of the array
  delete[] root->lvl;  //then the array - note the delete[]
  delete root;  //then the root.
}

int
main  (int ac, char **av)
{
  btree<char> b(2);
}