长安逸魂 2017-03-11 06:28 采纳率: 85.7%
浏览 1456

C++单链表定义及简单应用出错

跟着书上做单链表的应用,
首先先建立了一个danlianbiao.h文件
#include
#include
#include
using namespace std;
template
class ablist
{
public:
int Getlength() {return length;}
virtual type Get(int i);
virtual bool Set(type x,int i)=0;
virtual void MakeEmpty()=0;
virtual bool Insert(type value,int i)=0;
virtual type Remove(int i)=0;
virtual type Remove1(type value)=0;
protected:
int length;
};

//抽象节点类
template
class ListNode
{
public:
ListNode()
{
next=NULL;
}
ListNode(const type & item,ListNode *next1=NULL)
{
data=item;
next=next1;
}
type data;
ListNode *next;
};

//抽象链表类
template
class ablinklist:public ablist
{
public:
ListNode *GetHead()
{
return head;
}
ListNode *GetNext(ListNode & n)
{
return n.next==head?n.next->next:n.next;
}
type Get(int i);
bool Set(type x,int i);
ListNode *Find1(type value);
ListNode *Find (int i);
void MakeEmpty();
virtual bool Insert(type value, int i)=0;
virtual type Remove(int i)=0;
virtual type Remove1(type value)=0;
protected:
ListNode *head;
};

//设值函数
template
bool ablinklist::Set(type x,int i)
{
ListNode *p=Find(i);
if(p==NULL||p==head)
return false;
else p->data=x;
return true;
}

//取值函数
template
type ablinklist ::Get(int i)
{
ListNode *p=Find(i);
assert(p&&p!=head);
return p->data;
}

//清空链表函数
template
void ablinklist::MakeEmpty()
{
ListNode *q=head->next;
int i=1;
while(i++<=length)
{
head->next=q->next;
delete q;
q=head->next;
}
length=0;
}

//搜索值为value的结点
template
ListNode *ablinklist ::Find1(type value)
{
ListNode *p=head->next;
int i=1;
while (i++<=length && p->data!=value)
p=p->next;
return p;
}

//定位函数
template
ListNode *ablinklist::Find(int i)
{
if(ilength) return NULL;
if(i==0) return head;
ListNode *p =head->next;
int j=1;
while(p!=NULL&&j {
p=p->next;
j++;
}
return p;
}

//单链表类的定义
template
class List:public ablinklist
{
public:
List()
{
head=new ListNode;
length=0;
}
List(List & l)
{
Copy(l);
}
~List()
{
MakeEmpty();delete head;
}
bool Insert(type value,int i);
type Remove(int i);
type Remove1(type value);
List &Copy(List & l);
List &operator =(List &l);
friend ostream &operator <<(ostream &, List &);
};

//在第i个位置插入一个结点
template
bool List ::Insert(type value,int i)
{
ListNode *p=Find(i-1);
if(p==NULL)
return false;
ListNode *newnode =new ListNode (value,p->next);
assert(newnode);
p->next=newnode;
length++;
return true;
}

//删除指定位置i处的结点
template
type List ::Remove(int i)
{
ListNode *p=Find(i-1),*q;
assert(p&&p->next);
q=p->next;
p->next=q->next;
type value=q->data;
delete q;
length --;
return value;
}

//删除元素值为value的结点
template
type List ::Remove1(type value)
{
ListNode *q,*p=head;
while (p->next!=NULL&&p->next->data!=value)
p=p->next;
assert(p->next);
q=p->next;
p->next=q->next;
delete q;
length--;
return value;
}

//拷贝链表
template
List &List ::Copy(List & l)
{
ListNode *q,*p,&r;
length=l.length;
head=NULL;
if(!l.head)
return *this;
head=new ListNode ;
if(!head)
head->data=(l.head)->data;
head->next=NULL;
r=NULL;p=head;
q=l.head->next;
while (q)
{
r=new ListNode ;
if(!r)
return *THIS;
r->data=q->data;
r->next=NULL;
p->next=r;
p=p->next;
q=q->next;
}
return *this;
}

//重载赋值运算符
template
List & List ::operator=(List &l)
{
if(head)
MakeEmpty();
Copy(l);
return *this;
}
template
ostream & operator <<(ostream &out,List & l)
{
ListNode *p=l.head->next;
out<<"lrngth:"< while(p)
{
outdata<<" ";
p=p->next;
}
out<<"\n";
return out;
}

然后建立.cpp文件,下面是我从书上一大段代码里面选了一部分写的。

//#include "lianbiao.h"
#include
#include "danlianbiao.h"
#include "assert.h"
#include "string.h"
#include "stdio.h"
using namespace std;

struct term{
int coef;
int exp;
bool operator!=(term & t)
{return coef!=t.coef||exp!=t.exp;}
friend ostream &operator <<(ostream &out,term &t)
{
out< }
friend istream &operator>>(istream &in,term &t)
{
in>>t.coef>>t.exp;return in;}
};

void main()
{
List polya,polyb,polyc;
term ta,tb,t;
int na,nb;
int i,j;
cout<<"a多项式共有多少项?\n";
cin>>na;
cout<<"按指数从大到小的顺序,依次输入各项系数、指数\n";
for(i=1;i {
cout cin>>t;
polya.Insert(t,i);
}
cout<<"b多项式共有多少项?\n";
cin>>nb;
cout<<"按恪?指?数簓从洙?大洙?到?小?的?顺3序ò,\n依皑?次?输?入?各÷项?系μ数簓、¢指?数簓。£\n";
for(i=1;i<=nb;i++)
{
cout<<"\n第台"< cin>>t;
polyb.Insert(t,i);
}
cout<<"\npolya:";
cout<<polya;
cout<<"\npolyb";
cout<<polyb;
}

结果是无法运行,报错如下
d:\新建文件夹\chapter1\chapter1\polynomial_lianbiao.cpp(27): warning C4101: “ta”: 未引用的局部变量
1>d:\新建文件夹\chapter1\chapter1\polynomial_lianbiao.cpp(27): warning C4101: “tb”: 未引用的局部变量
1>d:\新建文件夹\chapter1\chapter1\polynomial_lianbiao.cpp(29): warning C4101: “j”: 未引用的局部变量
1> 正在生成代码...
1>polynomial_lianbiao.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class List &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$List@Uterm@@@@@Z),该符号在函数 _main 中被引用
1>polynomial_lianbiao.obj : error LNK2001: 无法解析的外部符号 "public: virtual struct term __thiscall ablist::Get(int)" (?Get@?$ablist@Uterm@@@@UAE?AUterm@@H@Z)
1>D:\新建文件夹\chapter1\Debug\chapter1.exe : fatal error LNK1120: 2 个无法解析的外部命令
1>
1>生成失败。

不知道哪里出的错误,又该如何解决啊??

  • 写回答

2条回答 默认 最新

  • devmiao 2017-03-11 06:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题