C++错误 使用类 模板 需要 模板 参数列表
#ifndef _CON_
#define _CON_
template <typename T>
struct Container
{
virtual Container& push_back(const T& data) = 0;
virtual size_t size() const = 0;
virtual operator bool() const = 0;
virtual void clear() = 0;
};
#endif
#ifndef __LIST__
#define __LIST__
#include "container.h"
#include "quad.h"
typedef void (*ACCESSFUN)(QUADPTR); //定义遍历时节点处理函数类型
struct Node
{
QUADPTR data;
Node *next;
};
template <typename T>
class List : public Container<T>
{
protected :
struct Node
{
T data;
Node *next;
Node(const T& q):data(q){}
}*head,*tail;
public:
List():head(NULL),tail(NULL){}
List(const List& l):head(l.head),tail(l.tail){}
~List(){clear();}
operator bool() const {return head!=NULL;}
void clear()
{
if(head = NULL)
return ;
Node *p=head,*q= head;
while(p != NULL)
{
q = p->next;
delete p;
p = q;
}
}
List& push_back(const T& data)
{
Node *p = new Node(data);
p->next = NULL;
if(tail == NULL)
head = tail = p;
else
{
tail->next = p;
tail = p;
}
return *this;
}
List& operator = (const List& s)
{
head = s.head;
tail = s.tail;
return *this;
}
List& operator+=(const T& data)
{
push_back(data);
return *this;
}
size_t size()const
{
Node *p = head;
size_t i = 0;
while (p != NULL)
{
p = p->next;
++i;
}
return i;
}
void traverse(ACCESSFUN f)
{
Node *p = head;
while (p != NULL)
{
f(p->data);
p = p->next;
}
}
friend class listIterator;
class listIterator
{
private:
List<T>* plist;
typename List<T>::Node *p;
public:
listIterator(const List<T>& list):plist(const_cast <List<T>*>(&list)),p(list.head){}
listIterator(const listIterator& itr):plist(itr.plist),p(itr.p){}
listIterator():plist(NULL),p(NULL){}
listIterator& begin(){p = plist->List<T>::head;return *this}
listIterator end() {return listIterator();}
listIterator& operator = (const listIterator& itr){plist = itr.plist;p = itr.p;return *this;}
bool operator != (const listIterator& itr){return p!=itr.p;}
listIterator& operator ++(){p = p->next; return *this;}
T& operator *(){return p->data;}
};
};
#endif
list.cpp
无内容
int main()
{
int choose=1;int choice=0;
int a,b,c;
double answer;
double m;
List list,list1;
while(choose==1)
{
int choice=rand()%4;
if (choice==0)
{
srand((int)time(0));
a=rand()%10+1;
b=rand()%10+1;
Rectangle *p=new Rectangle(a,b);
Rectangle *q=new Rectangle;
list.push_back(reinterpret_cast<QUADPTR>(p));
cout<<"图形及其数据 "<<endl;
m=p->area();
p->draw();
cout<<"你的答案:"<<endl;
cin>>answer;
if(answer==m)
cout<<"^_^正确"<<endl;
else
{
cout<<"#_# 你错了哦"<<endl;
cout<<"正确答案是 :"<<p->area()<<endl;
cout<<endl<<endl;
}
。。。
list.traverse(access);
system("pause");
return 0;
}
错误
c:\users\hp\desktop\source_3rd\source_2rd\source_4th.cpp(30): error C2955: “List”: 使用类 模板 需要 模板 参数列表
1> c:\users\hp\desktop\source_3rd\source_2rd\list.h(41) : 参见“List”的声明
1>c:\users\hp\desktop\source_3rd\source_2rd\source_4th.cpp(30): error C2133: “list”: 未知的大小
1>c:\users\hp\desktop\source_3rd\source_2rd\source_4th.cpp(30): error C2512: “List”: 没有合适的默认构造函数可用
1>c:\users\hp\desktop\source_3rd\source_2rd\source_4th.cpp(30): error C2133: “list1”: 未知的大小
1>c:\users\hp\desktop\source_3rd\source_2rd\source_4th.cpp(41): error C2662: “List<T>::push_back”: 不能将“this”指针从“List”转换为“List<T> &”
[解决办法]
看样子应该是
List<QUADPTR> list, list1;