未声明的标识符错误
#include<iostream>using namespace std;
struct element
{
int val;
element *next;
};
class list
{
element *elems;
public:
list(){elems=0;}
~list();
virtual bool insert(int val);
virtual bool deletes(int val);
bool contain(int val);
virtual void print();
};
class set:public list
{
int card;
public:
set(){card=0;};
bool insert(int val);
bool deletes(int val);
void print();
};
list::~list()
{
element *tmp=elems;
for(element *elem=elems;elem!=NULL;)
{
tmp=elem;
elem=elem->next;
delete tmp;
}
}
bool list::insert(int val)
{
elememt* elem=new element;
if(elem!=NULL)
{
elem->val=val;
elem->next=elems;
elems=elem;
return true;
}
else return false;
}
bool list::deletes(int val)
{
if(elems==NULL)
return false;
element *tmp=elems;
if(elems->val==val)
{
elems=elems->next;
delete tmp;
return true;
}
else
for(element *elem=elems;elem!=NULL;elem=elem->next)
if(elem->next->val==val)
{
tmp=elem->next;
elem->next=tmp->next;
delete tmp;
return true;
}
return false;
}
bool list::contain(int val)
{
if(elems==NULL)
return false;
if(elems->val==val)
return true;
else
for(element *elem=elems;elem!=NULL;elem=elem->next)
if(elem->next->val==val)
return true;
return false;
}
void list::print()
{
cout<<"调用基类的print()函数!"<<endl;
cout<<"链表元素为:";
if(elems==NULL)
return;
for(element *elem=elems;elem!=NULL;elem=elem->next)
cout<<elem->val<<" ";
cout<<endl;
}
bool set::insert(int val)
{
if(!contain(val) && list::insert(val))
{
++card;
return true;
}
return false;
}
bool set::deletes(int val)
{
if(list::deletes(val)){
--card;
return true;}
return false;
}
void set::print()
{
cout<<"调用派生类的print()函数!"<<endl;
cout<<"集合元素为:";
list::print();
cout<<"集合元素个数为:"<<card;
cout<<endl;
}
int main()
{
list *p,list1;
set set1;
p=&list1;
p->insert(12);
p->insert(34);
p->insert(56);
p->insert(78);
p->insert(90);
p->print();
p->deletes(56);
p->deletes(40);
p->print();
cout<<endl;
p=&set1;
p->insert(123);
p->insert(234);
p->insert(345);
p->insert(123);
p->print();
p->deletes(123);
p->deletes(456);
p->print();
return 0;
}
sdf.cpp
d:\文件\头文件\sdf\sdf\sdf.cpp(40) : error C2065: “elememt”: 未声明的标识符
d:\文件\头文件\sdf\sdf\sdf.cpp(40) : error C2065: “elem”: 未声明的标识符
d:\文件\头文件\sdf\sdf\sdf.cpp(41) : error C2065: “elem”: 未声明的标识符
d:\文件\头文件\sdf\sdf\sdf.cpp(43) : error C2065: “elem”: 未声明的标识符
d:\文件\头文件\sdf\sdf\sdf.cpp(43) : error C2227: “->val”的左边必须指向类/结构/联合/泛型类型
类型是“'unknown-type'”
d:\文件\头文件\sdf\sdf\sdf.cpp(44) : error C2065: “elem”: 未声明的标识符
d:\文件\头文件\sdf\sdf\sdf.cpp(44) : error C2227: “->next”的左边必须指向类/结构/联合/泛型类型
类型是“'unknown-type'”
d:\文件\头文件\sdf\sdf\sdf.cpp(45) : error C2065: “elem”: 未声明的标识符
生成日志保存在“file://d:\文件\头文件\sdf\sdf\Debug\BuildLog.htm”
sdf - 8 个错误,0 个警告