模板类的构造函数和析构函数声明语法

彼德

我试图制作一个实现链表的队列,但遇到编译器错误。错误来自我调用析构函数的行上的重载赋值运算符函数(标有全大写注释)。我有一个预感,这是一个简单的修复,与我的构造函数/析构函数声明的语法有关。

我得到的错误指出以下代码: 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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在模板类的构造函数末尾调用的模板的析构函数

我是否需要在新类中声明构造函数和析构函数?

模板的构造函数和析构函数的名称

模板类的析构函数

构造函数和析构函数

对象的构造函数和析构函数

GCC和clang中的奇怪构造函数和析构函数语法(void *返回类型)

从析构函数访问模板类构造函数的参数,可以吗?

在UML类图中为C ++类添加构造函数和析构函数

复制构造函数,析构函数和临时函数

类构造函数或析构函数之后的分号(;)

具有私有构造函数和析构函数的类对象的向量?

是否可以实例化具有删除的构造函数和析构函数的非聚合类?

为什么类的默认构造函数和析构函数是内联的?

我们是否需要设置move构造函数=默认值?对于过去使用用户声明的析构函数buildet和C ++ 98/03的旧类?

静态分配的构造函数和析构函数顺序

复制构造函数和析构函数的奇怪调用

静态成员变量的构造函数和析构函数(指针)

奇怪的复制构造函数和析构函数错误

对构造函数和析构函数的未定义引用

限制对C ++构造函数和析构函数的访问

关于结构构造函数和析构函数的行为-C ++

C ++中的析构函数和构造函数排序

构造函数和析构函数如何工作?

向量中的匿名构造函数和析构函数调用

构造函数和析构函数必须是虚拟的吗?

c ++复制构造函数和析构函数

为什么带有用户声明的析构函数的类具有隐式默认构造函数?

为什么在构造时调用C ++类的析构函数?