gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:44 54 4
gpt4 key购买 nike

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

我收到的错误说明了以下代码: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

最佳答案

查看此线程:Can i call destructor from its class method? .解决这个问题的一个简单方法是创建一个函数来清空队列,然后从析构函数和赋值运算符中调用它。

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();
}

关于c++ - 模板类的构造函数和析构函数声明语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26397319/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com