用模板实现双转移的问题
最近我试图用模板实现续集。这是导致用模板实现双转移的问题
#pragma once #include <iostream>
using namespace std;
template<typename Type>
struct Node {
Node();
Node<Type>* next = NULL;
Node<Type>* prev = NULL;
Type data;
};
template<typename Type>
class deque{
private:
Node<Type>* front = NULL;
Node<Type>* back = NULL;
public:
deque(Type key);
~deque();
Node<Type>* back();
Node<Type>* front();
int size();
bool empty();
void push_front(Type key);
void push_back(Type key);
void pop_front();
void pop_back();
void print_in_order();
};
template<typename Type>
deque<Type>::deque(Type key)
{
if (front != NULL || back != NULL)
return
else
{
front = new Node<Type>();
front->data = key;
prev = front;
}
}
template<typename Type>
deque<Type>::~deque()
{
Node<Type>* delPtr;
while (front->prev != NULL)
{
delPtr = front;
cout << "Deleted " << front->data<<endl;
front = front->prev;
delete delPtr;
}
back = NULL;
delete front;
}
template<typename Type>
Node<Type>* deque<Type>::back()
{
if(back!= NULL)
return back;
else
return NULL
}
template<class Type>
Node<Type>* deque<Type>::front()
{
return front;
}
template<typename Type>
int deque<Type>::size()
{
int counter = 0;
Node<Type>* temp = front;
while (temp->back != NULL)
{
counter++;
temp = temp->back;
}
return counter;
}
template<typename Type>
bool deque<Type>::empty()
{
if (front == NULL && back == NULL)
return true;
else
return false;
}
template<typename Type>
void deque<Type>::push_front(Type key)
{
Node<Type>* temp = new Node<Type>();
temp->data = key;
temp->prev = front;
front->next = temp;
front = temp;
}
template<typename Type>
void deque<Type>::push_back(Type key)
{
Node<Type>* temp = new Node<Type>();
temp->data = key;
temp->next = back;
back->prev = temp;
back = temp;
}
template<typename Type>
inline void deque<Type>::pop_front()
{
if (front != NULL) {
Node<Type>* delPtr = front;
front = front->prev;
front->next = NULL;
delete delPtr;
}
else
cout << "There is no front in empty deque" << endl;
}
template<typename Type>
void deque<Type>::pop_back()
{
if (back != NULL) {
Node<Type>* delPtr = back;
back = back->next;
back->prev = NULL;
delete delPtr;
}
else
cout << "There is no back in empty deque " << endl;
}
template<typename Type>
void deque<Type>::print_in_order()
{
if (front == NULL)
return;
Node<Type> temp = front;
while (temp->prev != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
template<typename Type>
inline Node<Type>::Node()
{
}
然后一些代码来检查是否正常工作
int main(){ deque<int> qq(1);
qq.push_front(2);
qq.push_front(3);
qq.print_in_order();
return 0;
}
此代码不希望编译。我试图寻找类似的项目或错误,并找不到有用的东西。因此,如果有人能够解释发生了什么问题,那将是非常好的。 大部分错误都是一样的。例如,不允许使用back()
或front()
,因为它们不是函数,也不是静态成员。或者back()
和front()
作为类模板的成员不能接收类型的函数。希望有人能帮忙。
回答:
有和
Node<Type>* front = NULL; Node<Type>* back = NULL;
和
Node<Type>* back(); Node<Type>* front();
之间的冲突,所以首先改变Node<Type>* front = NULL;
到Node<Type>* m_front = NULL;
同样以back
。有你的代码中的另一个错误,例如
Node<Type> temp = front;
必须
Node<Type>* temp = front;
最后,你可以看到一个没有编译错误的代码上ideone
好幸运!
以上是 用模板实现双转移的问题 的全部内容, 来源链接: utcz.com/qa/258374.html