我正在尝试在c ++中实现双向链表,现在我正在添加迭代器功能。一切都编译没有错误,并且(可能)一直很好,直到我添加这些方法:
template<class T>
typename UberList<T>::Iter UberList<T>::begin() const
{
Iter it;
it.curr = head;
return it;
}
template<class T>
typename UberList<T>::Iter UberList<T>::end() const
{
Iter it;
it.curr = tail;
return it;
}
然后我明白了:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
UberList2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'UberList<int>::Iter::Iter()' referenced from C:\USERS\HOME\CPPTEST\UBERLIST2.OBJ
我在这里看到类似的问题,它可能是一个链接问题,但我不确定这是不是我的情况,因为我只使用一个文件。我很感激有关这个奇怪问题的任何帮助。
这是我班级的完整代码:
#include <iostream>
using namespace std;
template<class T>
class UberList
{
friend class Iter;
private:
struct Node
{
friend class Iter;
T data;
Node *prev;
Node *next;
Node()
:data(0), prev(0), next(0)
{
}
Node(const T& d, Node *p, Node *n)
: data(d), prev(p), next(n)
{
}
};
public:
class Empty
{
};
class BadIter
{
};
class Iter
{
friend class UberList;
public:
//constructors
Iter();
Iter(Node *n)
{
curr = n;
}
T& operator*()
{
return curr->data;
}
Iter& operator++()
{
if(curr == 0)
throw BadIter();
curr = curr->next;
return *this;
}
Iter& operator--()
{
if(curr == 0)
throw BadIter();
curr = curr->prev;
return *this;
}
bool operator==(const Iter& it)
{
return curr == it.curr;
}
bool operator!=(const Iter& it)
{
return curr != it.curr;
}
private:
Node *curr;
};
UberList()
:length(0), head(0), tail(0)
{
}
~UberList();
int size()
{
return length;
}
bool empty()
{
return length == 0;
}
void pushBack(const T& elem);
void popBack();
void pushFront(const T& elem);
void popFront();
Iter begin() const;
Iter end() const;
Iter erase(typename UberList<T>::Iter curr);
void insertBefore(const T& elem, Iter curr);
private:
int length;
Node *head;
Node *tail;
};
template<class T>
typename UberList<T>::Iter UberList<T>::begin() const
{
Iter it;
it.curr = head;
return it;
}
template<class T>
typename UberList<T>::Iter UberList<T>::end() const
{
Iter it;
it.curr = tail;
return it;
}
template<class T>
UberList<T>::~UberList()
{
while(head != 0)
{
popBack();
}
}
template<class T>
void UberList<T>::pushBack(const T& elem)
{
if(empty())
{
head = tail = new Node(elem, 0, 0);
}
else
{
tail = tail -> next = new Node(elem, tail, 0);
}
++length;
}
template<class T>
void UberList<T>::popBack()
{
if(empty())
throw Empty();
if(length == 1)
{
delete head;
head = tail = 0;
}
else
{
tail = tail->prev;
delete tail->next;
tail->next = 0;
}
}
template<class T>
typename UberList<T>::Iter UberList<T>::erase(typename UberList<T>::Iter curr)
{
if(curr.curr == 0)
throw BadIter();
if(curr.curr == head)
{
popFront();
}
else if(curr.curr == tail)
{
popBack();
}
else
{
curr.curr->prev->next = curr.curr->next;
curr.curr->next->prev = curr.curr->prev;
Node *t = curr.curr->next;
delete curr.curr;
return Iter(t);
}
}
template<class T>
void UberList<T>::insertBefore(const T& elem, Iter curr)
{
if(curr.curr == 0)
throw BadIter();
if(curr.curr == head)
{
pushfront(elem);
}
else
{
curr.curr->prev = curr.curr->prev->next = new Node(elem, curr.curr->prev, curr.curr);
}
}
int main()
{
int a;
UberList<int> ulist;
while (cin >> a)
{
ulist.pushBack(a);
}
for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
{
cout << *it << " ";
}
return 0;
}
答案 0 :(得分:3)
声明了Iter默认构造函数但未定义。将第48行更改为
Iter(){curr = NULL; }