::Iter::Iter( )' referenced from C:\USERS\HOME\CPPTEST\UBERLIST.OBJ"-6ren"> ::Iter::Iter( )' referenced from C:\USERS\HOME\CPPTEST\UBERLIST.OBJ"-我正在尝试用 C++ 实现双向链表,现在我正在添加迭代器功能。在我添加这些方法之前,一切都没有错误地编译并且(可能)运行良好: template typename UberList::Iter Ube-6ren">
gpt4 book ai didi

C++ 问题: "Unresolved external ' UberList::Iter::Iter( )' referenced from C:\USERS\HOME\CPPTEST\UBERLIST.OBJ"

转载 作者:行者123 更新时间:2023-11-30 03:09:01 24 4
gpt4 key购买 nike

我正在尝试用 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

我在这里看到类似的问题,它可能是一个链接问题,但我不确定这是否是我的情况,因为我只使用一个文件。对于这个奇怪的问题,我将不胜感激。

这是我的类(class)的完整代码:

#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;
}

最佳答案

Iter 默认构造函数已声明但未定义。将第 48 行更改为

Iter() { 当前 = NULL;

关于C++ 问题: "Unresolved external ' UberList<int>::Iter::Iter( )' referenced from C:\USERS\HOME\CPPTEST\UBERLIST.OBJ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4470528/

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