(C ++错误:未分配指针)链接列表

时间:2013-10-14 21:30:33

标签: c++ pointers linked-list queue

有了这个主要内容:

void print_first(Queue q);

int main()
{
   Queue q1;
   Queue q2(4);
   q2.push(5);
   q2.push(3);
//   q2.print_list();
   for (int i = 0; i < 3; i++)
   {
      print_first(q2);
   }
   return 0;
}

void print_first(Queue q)
{
   if (!q.isEmpty())
   {
      cout << q.pop() << endl;
   }
   else
   {
       cout << "Nothing in queue" << endl;
   }
}

和类Queue中的这些函数定义,它有一个链接列表,其中“head”和“tail”作为链接列表中第一个和最后一个节点的指针,每个节点包含另一个存储重新定义的结构“数据” type ElementType:

bool Queue::push(ElementType newElement)
{
   tail->next = new Node;
   if (tail->next == NULL)
   {
      return false;
   }
   tail->next->data.value = newElement;
   tail->next->next = NULL;
   tail = tail->next;
   return true;
}

ElementType Queue::pop()
{
   Node *newNode;
   ElementType returnData;
   returnData = head->data.value;
   newNode = head;
   head = head->next;
   delete newNode;
   return returnData;
}

bool Queue::isEmpty()
{
   if(head == NULL)
   {
       return true;
   }
   return false;
}

为什么我收到此错误? 4 装配线模拟器(9701)malloc: *对象0x1001000e0的错误:未释放指针被释放 * 在malloc_error_break中设置断点以进行调试

1 个答案:

答案 0 :(得分:1)

拥有您展示的代码我认为问题在于您将队列q的副本传递给函数print_first

void print_first(Queue q)

如果你没有正确编写复制构造函数,那么你可能会遇到一些函数问题。

相关问题