C+++【处女贴】老师给的一个题目,写出来报错看不懂,请高手不吝指教
千里于野 2009-12-08 09:45:56 【题目】3. 定义元素类型为int、元素个数不受限制的集合类Set,包括:<=(包含于)、==(相等)、!=(不等)、 |(并集)、 &(交集)、-(差集)、+=(增加元素)、-=(删除元素)等操作
【VS2008报错】
1>------ 已启动生成: 项目: intSet, 配置: Debug Win32 ------
1>正在链接...
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用
1>D:\C++WorkPlace\C++ 郑滔\面向对象部分实习作业\3-intSet\intSet\Debug\intSet.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>生成日志保存在“file://d:\C++WorkPlace\C++ 郑滔\面向对象部分实习作业\3-intSet\intSet\intSet\Debug\BuildLog.htm”
1>intSet - 2 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
【VC6报错(按F5)】
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\Windows\System32\kernel32.dll', no matching symbolic information found.
Loaded 'C:\Windows\System32\KernelBase.dll', no matching symbolic information found.
HEAP[asd.exe]: Invalid address specified to RtlValidateHeap( 005D0000, 0012FEA8 )
【代码】
#include<iostream>
using namespace std;
//通过链表实现SET
class IntSet{
public:
struct IntNode{
int val;
IntNode *after;
};//链表节点
IntSet(){
head=NULL;
}
IntSet(int b){
IntNode tmp;
tmp.val=b;
this->head=(&tmp);
}
~IntSet(){
delete []head;
}
void operator +=(int b){
IntNode tmp;
tmp.val=b;
if(head==NULL)head=&tmp;//set为空
else if(head!=NULL&&b<head->val){
tmp.after=head;
head=&tmp;
}
else {
p=head;
for(;p&&p->val<b;p=p->after)q=p;
if(p->val!=b){
tmp.after=q->after;
q->after=&tmp;
}
if(p->val==b)free(&tmp);
}
}//增加元素,并且排序
void operator -=(int b){
p=head;
for(;p->val!=b;p=p->after)q=p;
if(p==NULL) cout<<b<<" is not in the set!";
else {
q->after=p->after;
delete p;
p=q->after;
}
}//删除元素
IntSet* operator |(IntSet b){
b.p=b.head;
for(;b.p;b.p=b.p->after)this->operator +=(b.p->val);
return this;
}//并集
IntSet* operator &(IntSet b){
this->p=this->head;
b.p=b.head;
while(this->p){
if(this->p->val==b.p->val){
this->p=this->p->after;
b.p=b.p->after;
}
if(this->p->val>b.p->val){
b.p=b.p->after;
}
if(this->p->val<b.p->val){
this->operator -=(this->p->val);
}
}
return this;
}//交集
IntSet* operator -(IntSet b){
this->p=this->head;
b.p=b.head;
while(this->p){
if(this->p->val==b.p->val){
this->operator -=(this->p->val);
b.p=b.p->after;
}
if(this->p->val>b.p->val){
b.p=b.p->after;
}
if(this->p->val<b.p->val){
this->p=this->p->after;
}
}
return this;
}//差集
bool operator ==(IntSet b){
this->p=this->head;
b.p=b.head;
while(this->p||b.p){
if(this->p->val!=b.p->after->val)
return 0;
this->p=this->p->after;
b.p=b.p->after;
}
return 1;
}//相等
bool operator <=(IntSet b){
return this==this->operator &(b);
}//包含于
bool operator !=(IntSet b){
return !(this->operator ==(b));
}//不等
void printSet(){
p=head;
while(p){
cout<<p->val<<" ";
p=p->after;
}
}
private:
IntNode *head,*p,*q;
};
int main(){
IntSet *a=new IntSet();
a->operator +=(2);
a->operator +=(3);
a->operator +=(4);
a->operator +=(5);
a->operator +=(6);
a->operator +=(7);
a->operator +=(8);
a->printSet();
a->operator -=(6);
a->operator -=(7);
a->printSet();
a->operator -=(3);
a->operator -=(5);
a->printSet();
return 0;
}