如何在头文件中实现的构造函数中将指向模板类的链表指针初始化为NULL
How to initialize pointers in a linked list to templated class to NULL in constructor implemented in header file
你好,我正在做一个数据结构项目,我被卡住了。该项目将构建一个模板化的链表类和一个嵌套的迭代器类,仅在头文件中。我不明白如何在模板化的LinkedList类中使用迭代器类。我对一般的迭代器不太确定。声明的方法都是运行将要运行的测试所必需的。我目前被困在初始化'LinkedList* prev'和下一步的构造函数。请帮助!如何初始化模板化的东西?编译器想知道我认为什么数据类型?谢谢你的宝贵时间错误出现在LinkedList类的构造函数声明中"从'int'类型的右值初始化'std::basic_string char&'类型的非const引用无效"是错误消息
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template <typename T> class LinkedList
{
private:
LinkedList<T>* head;
int size;
T element = NULL;
LinkedList<T>* prev;
LinkedList<T>* next;
public:
class Iterator
{
friend class LinkedList<T>;
private:
public:
Iterator();
T operator*() const;
Iterator& operator++();
bool operator==(Iterator const& rhs);
bool operator!=(Iterator const& rhs);
};
LinkedList<T>():prev(0), next(0){} //HERE is the ERROR????////////
Iterator begin() const
{
// Iterator tempIterator = LinkedList<T>();
//
// return tempIterator->front;
return NULL;
}
Iterator end() const{return NULL;}
bool isEmpty() const;
T getFront() const;
T getBack() const;
void enqueue(T newElement)
{
LinkedList<T>* newElem = new LinkedList<T>();
newElem->element = newElement;
if(front == 0)
{
front = newElem;
}
else
{
back->next = newElem;
}
back = newElem;
listSize++;
}
void dequeue()
{
LinkedList<T>* tempElem = new LinkedList<T>();
if(front == 0){
cout << "List is empty!" << endl;
}
else
{
tempElem = front;
front = front->next;
cout << "the element dequeued: " << tempElem->element;
delete tempElem;
}
}
void pop();
void clear();
bool contains(T element) const;
void remove(T element);
};
#endif /* LINKEDLIST_H_ */
构造函数语法错误。
LinkedList():prev(0), next(0){}
这是正确的构造函数语法。一旦声明了template
类和参数,在template
声明中,仅模板的名称(不含参数)在其作用域中引用模板本身。类似地,如果要显式定义析构函数,则为:
~LinkedList() { /* ... */ }
如果你只想在模板声明中声明构造函数:
LinkedList();
,然后在模板声明之外定义它,那么正确的语法是:
template<typename T>
LinkedList<T>::LinkedList() :prev(0), next(0){}
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 指针永远不会在链表深层复制构造函数中达到 null
- 链表指向 NULL,尽管我已经初始化了这个链表
- 链表:最后一个指针不是NULL
- 为什么一个循环需要电流>链接 !=NULL 和一个电流 != NULL 作为我的链表?
- 添加到链表的节点在函数后恢复为 NULL
- 遍历链表: while(ptr!=NULL) vs while(ptr->next!=NULL)?
- 数据在null节点中的链表
- C++:为链表实现插入函数,prev_ptr最终为NULL
- 每次在链表中调用insert_node()函数时,Head都被赋值为Null
- 如何在头文件中实现的构造函数中将指向模板类的链表指针初始化为NULL
- 为什么我对指针>next == NULL 的检查在我的链表中出现错误?