| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 672 人关注过本帖
标题:这个错误怎么解决?
只看楼主 加入收藏
偶成
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-3-4
收藏
 问题点数:0 回复次数:2 
这个错误怎么解决?

这个程序的错误是加了最后一行 cout<<dq; 就跳出错误,没有那行就没问题的.
#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;

template<class type>
struct nodetype
{
type info;
nodetype<type> *link;
};
template<class type>
class linklist{
friend ostream& operator<<(ostream&,const linklist<type>&);//重载<<运算符
public:
const linklist<type>& operator=(const linklist<type>&);//重载=运算符
void initializelist();//初始化函数
bool isempty();//链表是否为空
int length();//返回链表的节点数
void destorylist();//删除链表所有节点,头尾指针设置为0
type front();//返回链表第一个节点元素
type back();//返回链表最后一个节点元素
bool search(const type& searchtitem);//确定某个元素是否在链表中
void insertfirst(const type& newitem);//在表前插入节点
void insertlast(const type& newitem);//在表后插入节点
void deletenode(const type& deleteitem);//删除表中的某个点
linklist();//默认构造函数
linklist(const linklist<type>& otherlist);//拷贝函数
~linklist();//析构函数
protected:
int count; //链表的节点数
nodetype<type> *first; //链表头指针
nodetype<type> *last; //链表尾指针
private:
void copylist(const linklist<type>& otherlist);//复制表otherlist给表
};

template<class type>
bool linklist<type>::isempty(){return (first==NULL);}//定义函数判断链表是否为空

template<class type>
linklist<type>::linklist(){//定义默认构造函数
first=NULL;
last=NULL;
count=0;
}

template<class type>
void linklist<type>::destorylist(){//定义函数,清空链表
nodetype<type> *temp;
while(first!=NULL){
temp=first;
first=first->link;
delete temp;
}
last=NULL;
count=0;
}
template<class type>
void linklist<type>::initializelist(){destorylist();}//定义函数,初始化链表

template<class type>
ostream& operator<<(ostream& out,const linklist<type>& list){//重载运算符<<函数
nodetype<type> *current;
current=list.first;
while(current != NULL){
out<<current->info<<" ";
current=current->link;
}
return out;
}

template<class type>
int linklist<type>::length(){return count;}//定义函数,返回链表节点数

template<class type>
type linklist<type>::front(){//定义函数,返回第一个节点
assert(last != NULL);
return first->info;
}

template<class type>
type linklist<type>::back(){//定义函数,返回最后一个节点
assert(last != NULL);
return last->info;
}

template<class type>
bool linklist<type>::search(const type& searchitem){//定义函数,查找一个元素是否在表中
nodetype<type> *current;
bool found;
current=first;
found=false;
while(current!=NULL && !found)
if(current->info==searchitem)
found=true;
else
current=current->link;
return found;
}

template<class type>
void linklist<type>::insertfirst(const type& newitem){//定义函数,在表头部插入节点
nodetype<type> *newnode;
newnode=new nodetype<type>;
assert(newnode != NULL);
newnode->info=newitem;
newnode->link=first;
first=newnode;
count++;
if(last==NULL)
last=newnode;
}

template<class type>
void linklist<type>::insertlast(const type& newitem){//定义函数,在表尾部插入节点
nodetype<type> *newnode;
newnode=new nodetype<type>;
assert(newnode != NULL);
newnode->info=newitem;
newnode->link=NULL;
if(first==NULL){
first=newnode;
last=newnode;
count++;
}
else{
last->link=newnode;
last=newnode;
count++;
}
}

template<class type>
void linklist<type>::deletenode(const type& deleteitem){//定义函数,删除表中某个节点
nodetype<type> *current;
nodetype<type> *trailcurrent;
bool found;
if(first==NULL)
cerr<<"cannot delete from an empty list.\n";
else{
if(first->info==deleteitem){
current=first;
first=first->link;
count--;
if(first==NULL)
last=NULL;
delete current;

}
else{
found=false;
trailcurrent=first;
current=first->link;
while(current!=null && !found){
if(current->info!=deleteitem){
trailcurrent=current;
current=current->link;
}
else
found=true;
}
if(found)
{
trailcurrent->link=current->link;
count--;
if(last==current)
last=trailcurrent;
delete current;
}
else
cout<<"item to be deleted is not in the list."<<endl;
}
}
}

template<class type>
void linklist<type>::copylist(const linklist<type>& otherlist){//定义函数,复制已知表给当前表
nodetype<type> *newnode;
nodetype<type> *current;
if(first!=NULL)
destorylist();
if(otherlist.first==NULL){
first=NULL;
last=NULL;
count=0;
}
else{
current=otherlist.first;
count=otherlist.count;
first=new nodetype<type>;
assert(first!=NULL);
first->info=current->info;
first->link=NULL;
last=first;
current=current->link;
while(current!=NULL){
newnode=new nodetype<type>;
assert(newnode!=NULL);
newnode->info=current->info;
newnode->link=NULL;
last->link=newnode;
last=newnode;
current=current->link;
}
}
}

template<class type>
linklist<type>::~linklist(){destorylist();}//定义析构函数

template<class type>
linklist<type>::linklist(const linklist<type>& otherlist){//定义复制构造函数
first=NULL;
copylist(otherlist);
}

template<class type>
const linklist<type>& linklist<type>::operator=(const linklist<type>& otherlist){//重载运算符=函数
if(this!=&otherlist)
copylist(otherlist);
return *this;
}


int _tmain(int argc, _TCHAR* argv[])
{
linklist<int> dq,dq2;
dq.insertfirst(30);
dq.insertfirst(50);
dq.insertfirst(100);
dq.insertfirst(80);
dq.insertlast(250);
dq2=dq;
cout<<dq;
return 0;
}

错误提示是这样的:
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class linklist<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$linklist@H@@@Z),该符号在函数 _wmain 中被引用 lianxi.obj
错误 2 fatal error LNK1120: 1 个无法解析的外部命令 E:\Documents and Settings\Visual Studio 2005\Projects\lianxi\Debug\lianxi.exe 1

搜索更多相关主题的帖子: type linklist 运算符 operator 
2006-06-11 09:09
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
try this one:
[CODE]
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;


template<class type>
struct nodetype
{
type info;
nodetype<type> * link;
};

template<class type>
class linklist;

template<class type>
ostream & operator<< (ostream & out, const linklist<type>& list);

template<class type>
class linklist
{
public:
linklist();//ĬÈϹ¹Ô캯Êý
linklist(const linklist<type>& otherlist);//¿½±´º¯Êý
const linklist<type>& operator=(const linklist<type>&);//ÖØÔØ=ÔËËã·û
bool isempty();//Á´±íÊÇ·ñΪ¿Õ
int length();//·µ»ØÁ´±íµÄ½ÚµãÊý
type front();//·µ»ØÁ´±íµÚÒ»¸ö½ÚµãÔªËØ
type back();//·µ»ØÁ´±í×îºóÒ»¸ö½ÚµãÔªËØ
bool search(const type& searchtitem);//È·¶¨Ä³¸öÔªËØÊÇ·ñÔÚÁ´±íÖÐ
void insertfirst(const type& newitem);//ÔÚ±íÇ°²åÈë½Úµã
void insertlast(const type& newitem);//ÔÚ±íºó²åÈë½Úµã
void deletenode(const type& deleteitem);//ɾ³ý±íÖеÄij¸öµã
~linklist();//Îö¹¹º¯Êý
private:
int count; //Á´±íµÄ½ÚµãÊý
nodetype<type> * first; //Á´±íÍ·Ö¸Õë
nodetype<type> * last; //Á´±íβָÕë
void destorylist();//ɾ³ýÁ´±íËùÓнڵã,ͷβָÕëÉèÖÃΪ0
void copylist (const linklist<type> &);
friend ostream & operator<< <>(ostream &,const linklist<type>&);//ÖØÔØ<<ÔËËã·û
};

template<class type>
linklist<type>::linklist() //¶¨ÒåĬÈϹ¹Ô캯Êý
{
first = NULL;
last = NULL;
count=0;
}

template<class type>
void linklist<type>::copylist(const linklist<type>& otherlist)
{
//¶¨Ò庯Êý,¸´ÖÆÒÑÖª±í¸øµ±Ç°±í
nodetype<type> *newnode;
nodetype<type> *current;
if(first!=NULL)
destorylist();
if(otherlist.first==NULL)
{
first=NULL;
last=NULL;
count=0;
}
else
{
current=otherlist.first;
count=otherlist.count;
first=new nodetype<type>;
assert(first!=NULL);
first->info=current->info;
first->link=NULL;
last=first;
current=current->link;
while(current!=NULL)
{
newnode=new nodetype<type>;
assert(newnode!=NULL);
newnode->info=current->info;
newnode->link=NULL;
last->link=newnode;
last=newnode;
current=current->link;
}
}
}

template<class type>
void linklist<type>::destorylist()
{
//¶¨Ò庯Êý,Çå¿ÕÁ´±í
nodetype<type> *temp;
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
count=0;
}

template<class type>
linklist<type>::linklist(const linklist<type>& anotherlist) //¶¨Ò帴Öƹ¹Ô캯Êý
{
copylist(anotherlist);
}

template<class type>
linklist<type>::~linklist() //¶¨ÒåÎö¹¹º¯Êý
{
destorylist();
}


template<class type>
const linklist<type>& linklist<type>::operator=(const linklist<type>& otherlist)
{ //ÖØÔØÔËËã·û=º¯Êý
if(this!=&otherlist)
copylist(otherlist);
return *this;
}

template<class type>
bool linklist<type>::isempty(){return (first==NULL);}//¶¨Ò庯ÊýÅжÏÁ´±íÊÇ·ñΪ¿Õ

template<class type>
int linklist<type>::length(){return count;}//¶¨Ò庯Êý,·µ»ØÁ´±í½ÚµãÊý

template<class type>
type linklist<type>::front()
{ //¶¨Ò庯Êý,·µ»ØµÚÒ»¸ö½Úµã
assert(last != NULL);
return first->info;
}

template<class type>
type linklist<type>::back()
{ //¶¨Ò庯Êý,·µ»Ø×îºóÒ»¸ö½Úµã
assert(last != NULL);
return last->info;
}

template<class type>
bool linklist<type>::search(const type& searchitem)
{ //¶¨Ò庯Êý,²éÕÒÒ»¸öÔªËØÊÇ·ñÔÚ±íÖÐ
nodetype<type> *current;
bool found;
current=first;
found=false;
while(current!=NULL && !found)
{
if(current->info==searchitem)
found=true;
else
current=current->link;
}
return found;
}

template<class type>
void linklist<type>::insertfirst(const type& newitem)
{ //¶¨Ò庯Êý,ÔÚ±íÍ·²¿²åÈë½Úµã
nodetype<type> *newnode;
newnode=new nodetype<type>;
assert(newnode != NULL);
newnode->info=newitem;
newnode->link=first;
first=newnode;
count++;
if(last==NULL)
last=newnode;
}

template<class type>
void linklist<type>::insertlast(const type& newitem)
{ //¶¨Ò庯Êý,ÔÚ±íβ²¿²åÈë½Úµã
nodetype<type> *newnode;
newnode=new nodetype<type>;
assert(newnode != NULL);
newnode->info=newitem;
newnode->link=NULL;
if(first==NULL)
{
first=newnode;
last=newnode;
count++;
}
else
{
last->link=newnode;
last=newnode;
count++;
}
}

template<class type>
void linklist<type>::deletenode(const type& deleteitem)
{ //¶¨Ò庯Êý,ɾ³ý±íÖÐij¸ö½Úµã
nodetype<type> *current;
nodetype<type> *trailcurrent;
bool found;
if(first==NULL)
cerr<<"cannot delete from an empty list.\n";
else
{
if(first->info==deleteitem)
{
current=first;
first=first->link;
count--;
if(first==NULL)
last=NULL;
delete current;
}
else
{
found=false;
trailcurrent=first;
current=first->link;
while(current!=NULL && !found)
{
if(current->info!=deleteitem)
{
trailcurrent=current;
current=current->link;
}
else
found=true;
}
if(found)
{
trailcurrent->link=current->link;
count--;
if(last==current)
last=trailcurrent;
delete current;
}
else
cout<<"item to be deleted is not in the list."<<endl;
}
}
}

template<class type>
ostream & operator<< (ostream & out, const linklist<type>& list)
{//ÖØÔØÔËËã·û<<º¯Êý
nodetype<type> * current =list.first;
while(current != NULL)
{
out<<current->info<<" ";
current=current->link;
}
return out;
}


int main(int argc, char * argv)
{
linklist<int> dq;
dq.insertfirst(20);
linklist<int> dq2;
dq.insertfirst(30);
dq.insertfirst(50);
dq.insertfirst(100);
dq.insertfirst(80);
dq.insertlast(250);
dq2=dq;
cout<<dq2<<endl;
cout<<dq<<endl;

system("pause");
return 0;
}

[/CODE]

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-06-11 12:13
偶成
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-3-4
收藏
得分:0 

还是有一个错误,不过我看了你的程序后,我把我原来哪个程序中
friend ostream & operator<<(ostream &,const linklist<type>&);
改成 friend ostream & operator<< <>(ostream &,const linklist<type>&);就没错了
这是为什么?


几年前,丢失了求知欲,游荡在游戏世界中. 而今寻找回了求知欲,希望通过自学充实自己.努力中...
2006-06-12 06:29
快速回复:这个错误怎么解决?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036403 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved