求c++高手解答。错误是List使用类模板需要参数列表;List<T>::Iterator没有合适构造函数可用。
答案:1 悬赏:40
解决时间 2021-04-17 19:05
- 提问者网友:追忆成伤
- 2021-04-17 15:55
#pragma once
using namespace std;
头文件略。
template<typename T>
class List
{
protected:
struct _Node
{
T data;
string name;
_Node* next;
_Node(const T& q):data(q){};
}*head,*tail;//链表的头尾指针
typedef _Node* _range;
size_t len;
public:
List(){head=tail=NULL;len=0;}//平凡构造函数
List(const List& l)
List& operator=(List& l)
List& operator+=(const T& quad)
~List()
size_t size()
void traverse(void(*f)(const T&))
T& operator[](int index)
void push_back(const T& quad)
void pop_back() //由于字数原因,以上函数体略
friend class Iterator ;
class Iterator
{
protected:
List<T>*plist;
typename List<T>::Node *p;
public:
Iterator(const List<T>&list):plist(const_cast<List<T>*>(&List)),p(list.head){}
Iterator(const Iterator& itr):plist(itr.plist),p(itr.p){}
Iterator():plist(NULL),p(NULL){}
Iterator&begin(){p=plist->List<T>::head;return *this;}
Iterator end(){return Iterator();}
Iterator&operator=(const Iterator& itr){plist=itr.plist;p=itr.p;return *this;}
Iterator&operator++(){p=p->next;return *this;}
Iterator&operator+(int i){p=p+i;return *this;}
bool operator!=(const Iterator& itr){return p!=itr.p;}
T&operator*(){return p->data;}
};
};
用到的地方为
POINT vert[4];
int shape;
List::Iterator itr, begin = list.begin(), end = list.end();
if (isLearning) begin = begin + (list.size() - 1); 只截取了部分
还有一个错误是begin,end不是List<T>的成员
最佳答案
- 二级知识专家网友:高冷不撩人
- 2021-04-17 16:53
你定义List是个模板类,T必须用具体的类型来实例化,比如T实例化成int
List<int> list =
List<int>::Iterator begin = list.begin()
当然你也可以自定义一个类型来实例化
class MyType
{
......
}
List<MyType> list =
List<MyType>::Iterator begin = list.begin();
List<int> list =
List<int>::Iterator begin = list.begin()
当然你也可以自定义一个类型来实例化
class MyType
{
......
}
List<MyType> list =
List<MyType>::Iterator begin = list.begin();
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯