c++ - 帮助链接列表模板

标签 c++ g++ linked-list

我的链表需要一些帮助。我认为问题出在复制构造函数或赋值重载中。当我打电话时,它一直给我的段错误: (队列 test_int_copy(test_int);) 您看到的任何其他错误或实现不当也会非常有帮助。 .h 文件

#ifndef QUEUE_H
#define QUEUE_H

template <class Object>
class Queue
{
 public:
     Queue();
     //initializes empty queue

     Queue(const Queue& a_queue);
     //copy constructor

     Queue& operator =(const Queue& rhs);
     //overload assignment operator

     bool enqueue(const Object& d);
     //insert object into queue,return true
     //return false if not

     bool dequeue(Object& d);
     //remove object from queue,return true
     //if empty return false

     bool isEmpty();
     //check if empty

     ~Queue();
     //destructor

private:
    struct ListNode
    {
        Object obj;
        //object that is in the list

        ListNode *next;
        //pointer to the next node

    };
    //struct of list

    ListNode *head;
    //pointer that points to head
};

#endif //Queue_H
#include "queue.cpp"  //include queue.cpp with file

.cpp 文件

     #include <iostream>
using namespace std;

template <class Object>
Queue<Object>::Queue()
{
    head = NULL;
}

template <class Object>
Queue<Object>::Queue(const Queue<Object> &a_queue)
{
    head = NULL;
  ListNode *nodePtr = a_queue.head;
    while (nodePtr){
      enqueue(nodePtr->obj);
      nodePtr = nodePtr->next;
    }

}

template <class Object>
Queue<Object>& Queue<Object>::operator =(const Queue<Object> &rhs)
{
    //head = NULL;
  ListNode *nodePtr = rhs.head;
  Object temp;
    while(head){
      dequeue(temp);
    }
    while (nodePtr){
      enqueue(nodePtr->obj);
      nodePtr = nodePtr->next;
    }
}

template <class Object>
bool Queue<Object>::enqueue (const Object& d) //Enqueue
{
    ListNode *newNode = new ListNode;
    newNode->obj = d;
    newNode->next = NULL;
    ListNode *nodePtr = NULL;
    ListNode *previousNode = NULL;

    if(isEmpty()){
        head = newNode;
        return true;
        }
    else{
        nodePtr = head;
        while(nodePtr != NULL){
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }
        if(previousNode->next == NULL){
            previousNode->next = newNode;
            return true;
        }
        else
            return false;
    }
}

template <class Object>
bool Queue<Object>::dequeue (Object& d)  //Dequeue
{
    ListNode *nodePtr;

    if(!head)
        return false;
    else{
      if(head->next != NULL){
            nodePtr = head;
            d = nodePtr->obj;
            head = head->next;
            delete nodePtr;
            return true;
      }
      else{
        d = head->obj;
        head = NULL;
        return true;
      }
    }
}


template <class Object>
bool Queue<Object>::isEmpty() //Check if Empty
{
    if(!head)
        return true;
    else
        return false;
}

template <class Object>
Queue<Object>::~Queue()   //Destructor
{
    Object temp;
    while(head)
        dequeue (temp);
}

最佳答案

也不要像这样包含源文件。或者,如果您坚持,请确保将其放入 include guard 中。例如

#include "queue.cpp"  //include queue.cpp with file
#endif //Queue_H

否则,如果 header 被多次包含(这种情况经常发生),您将遇到编译错误。 (STuff 将被定义多次)。

此外,如果您要包含 header 中的“源代码”——确保所有函数都是“内联的”(隐式或显式),否则您将遇到链接器错误

关于c++ - 帮助链接列表模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5589956/

相关文章:

python - 为什么此链接列表代码在 HackerRank 中显示错误?

c++ - 崩溃,同时打印链表的内容

c++ - 从 lambda (C++) 创建的 std::function 的奇怪返回行为

c++ - 获取对 std::thread::_M_start_thread 的 undefined reference

c++ - 如何在 VTK 中制作简单的二维等高线图?

c++ - Windows 上的链接 boost 问题

c++ - 我的 GUI 表单后面有烦人的空白控制台

python - 超出分区列表问题的时间限制

c++ - rand() 总是在每次运行时输出相同的数字

c++ - 在 CSplitterWnd 中设置事件面板