簡體   English   中英

模板類的構造函數和析構函數聲明語法

[英]Constructor and Destructor Declaration Syntax with Template Class

我正在嘗試建立一個實現鏈表的隊列,但遇到編譯器錯誤。 錯誤來自我調用析構函數的行上的重載賦值運算符函數(標有全大寫注釋)。 我有一個預感,這是一個簡單的修復,與我的構造函數/析構函數聲明的語法有關。

我得到的錯誤指出以下代碼: error C2512: 'Queue<char>::Queue' : no appropriate default constructor available

它沒有提到構造函數,但是它所指的那一行是下面我試圖調用析構函數的那一行。

在此先感謝您的幫助。

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;

template <class Type>
class Queue     // Create a Queue data structure implementing a linked list
{
    private:        // The private members
        struct Cell     // The Cell class will be the blueprints for each link in the list
        {
            Type data;      // The information held by the cell
            Cell* next;     // The link to the next cell
        };

        Cell* first = NULL;
        Cell* last = NULL;


    public:     // The public members
        Queue(Type);
        bool isEmpty();
        void push(Type);
        Type pop();
        Queue<Type>& operator=(Queue<Type>&);
        friend ostream& operator<<(ostream&, const Queue<Type>&);
        ~Queue();
};


template<class Type>
Queue<Type>::Queue(Type inputData)      // Constructor that initializes the queue with a new cell that last and first point to
{
    first = new Cell;

    first->data = inputData;
    first->next = NULL;

    last = first;
}




template<class Type>
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue)     // Overload "=" so that it performs a deep copy of a Queue object
{
    if (!queue.isEmpty())
    {
        ~Queue();      // HERE IS THE ERROR LINE

        Cell* rhs = queue.first;

        while (rhs != NULL)
        {
            push(rhs->data);
            rhs = rhs->next;
        }
    }

    return *this;
}




template<class Type>
Queue<Type>::~Queue()       // Destructor that deallocates all of the memory used by the queue.
{
    if (!isEmpty())     // We only need to deallocate the queue if it is non-empty
    {
        Cell *link = last;

        while (link != NULL)        // Until we reach the end of the queue, keep deleting each link
        {
            pop();
        }

        first = NULL;
        last = NULL;
    }
    else        // If the queue is already empty, let the user know
    {
        cout << "Cannot call destructor. The list is already empty.\n";
    }
}
#endif

看看這個線程: 我可以從其類方法中調用析構函數嗎? 解決此問題的一種簡單方法是使函數清空隊列,然后從析構函數和賦值運算符調用它。

template<class Type>
void Queue<Type> empty(){
    if (!isEmpty())     // We only need to deallocate the queue if it is non-empty
    {
         Cell *link = last;

        while (link != NULL)        // Until we reach the end of the queue, keep deleting each link
        {
            pop();
        }

        first = NULL;
        last = NULL;
    }
    else        // If the queue is already empty, let the user know
    {
        cout << "Cannot call empty. The list is already empty.\n";
    }
}

template<class Type>
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue)     // Overload "=" so that it performs a deep copy of a Queue object
{
    if (!queue.isEmpty())
    {
        empty();      // Tada, no more error

        Cell* rhs = queue.first;

        while (rhs != NULL)
        {
            push(rhs->data);
            rhs = rhs->next;
        }
    }

    return *this;
}




template<class Type>
Queue<Type>::~Queue()       // Deconstructor that deallocates all of the memory used by the queue.
{
    empty();
}

這與模板無關。

如果為類聲明任何構造函數,則將刪除編譯器綜合的默認構造函數(即不帶arg的默認構造函數)。

您必須自己定義Queue()

順便說一句,在全局范圍內使用using指令不是一個好主意。

我猜你定義了沒有參數的隊列,例如

Queue<char> quCh;

如果要執行此操作,則必須定義不帶參數的構造函數。

Queue();

或者您必須像這樣定義隊列:

Queue<char> quCh('a');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM