error LNK2019错误

卢队长 2011-09-27 03:05:31
/* 主函数*/
#include <iostream>
#include "CircularList.h"

using namespace std;

int main()
{
CircularList<int> list;
for(int i=0;i<20;i++){
list.Insert(i*3,i);
}
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();
for(int i=0;i<5;i++){
list.Insert(3,i*3);
}
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();

list.Remove(5);
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();

list.RemoveAll(3);
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();

cout<<"The third element is "<<list.Get(3)<<endl;

list.MakeEmpty();
cout<<"the Length of the list is "<<list.Length()<<endl;
list.Print();


return 0;
}


/* CircularList类*/
#pragma once

#include"ListNode.h"

template<typename Type>
class CircularList
{
public:
CircularList();
~CircularList();
void MakeEmpty(); //clear the list
int Length(); //get the length
ListNode<Type> *Find( int n ); //find the nth data
bool Insert( Type item, int n = 0 ); //insert the data into the nth data of the list
Type Remove( int n = 0 ); //delete the nth data
bool RemoveAll( Type item ); //delete all the datas which are equal to value
Type Get( int n ); //get the nth data
void Print(); //print the list
private:
ListNode<Type> *head;
};

#include "CircularList.h"

//构造函数
template<typename Type>
CircularList<Type>::CircularList():head( new ListNode<Type>() )
{
head ->m_pnext = head;
}

//析构函数
template<typename Type>
CircularList<Type>::~CircularList()
{
MakeEmpty();
delete head;
}


//将链表置空
template<typename Type>
void CircularList<Type>::MakeEmpty()
{
ListNode<Type> *pdel;
ListNode<Type> *pmove = head;

while( pmove -> m_pnext != head )
{
pdel = pmove -> m_pnext;
pmove -> m_pnext = pdel -> m_pnext;
delete pdel;
}
}


//返回链表的长度
template<typename Type>
int CircularList<Type>::Length()
{
ListNode<Type> *pmove = head;
int count = 0;

while( pmove ->m_pnext != head )
{
pmove = pmove ->m_pnext;
count++;
}
return count;
}


//寻找链表中的第n个元素
template<typename Type>
ListNode<Type>* CircularList<Type>::Find( int n = 0 )
{
if( n < 0 )
{
cout << "The n is out of boundary" << endl;
return NULL;
}

ListNode<Type> *pmove = head ->m_pnext;

for( int i = 0; ( i < n )&&(pmove != head); i++ )
{
pmove = pmove -> m_pnext;
}

if( pmove == head )
{
cout << "The n is out of boundary" << endl;
return NULL;
}

return pmove;
}

//在第n个位置插入元素
template<typename Type>
bool CircularList<Type>::Insert( Type value, int n = 0 )
{
if( n < 0 )
{
cout << "The n is out of boundary" << endl;
return false;
}

ListNode<Type> *pmove = head;
ListNode<Type> *pnode = new ListNode<Type>(value);

if( pnode == NULL )
{
cout << "Application error!!!" << endl;
exit(1);
}

for( int i = 0; i < n ; i++ )
{
pmove = pmove -> m_pnext;

if( pmove == head )
{
cout << "The n is out of boundary" << endl;
return false;
}
}

return true;
}

//删除第n个元素
template<typename Type>
Type CircularList<Type>::Remove( int n = 0 )
{
if( n < 0 )
{
cout << "The n is out of boundary" << endl;
exit(1);
}

ListNode<Type> *pmove = head;
ListNode<Type> *pdel;

for( int i = 0; i < n; i++ )
{
pmove = pmove ->m_pnext;

if( pmove == head )
{
cout << "The n is out of boundary" << endl;
exit(1);
}
}

pdel = pmove ->m_pnext;
pmove ->m_pnext = pdel ->m_pnext;
Type temp = pdel ->m_data;

delete pdel;
return temp;
}

//寻找链表中是否有元素值item
template<typename Type>
bool CircularList<Type>::RemoveAll( Type item )
{
ListNode<Type> *pmove = head;
ListNode<Type> *pdel = head ->m_pnext;

while( pdel != head )
{
if( pdel ->m_data == item )
{
pmove ->m_pnext = pdel ->m_pnext;
delete pdel;

pdel = pmove ->m_pnext;
continue;
}
pmove = pmove ->m_pnext;
pdel = pdel ->m_pnext;
}
return true;
}

//读取链表中第n个元素的元素值
template<typename Type>
Type CircularList<Type>::Get( int n = 0 )
{
if( n < 0 )
{
cout << "The n is out of boundary" << endl;
exit(1);
}

ListNode<Type> *pmove = head;

for( int i = 0; i < n; i++ )
{
pmove = pmove ->m_pnext;

if( pmove == head )
{
cout << "The n is out of boundary" << endl;
exit(1);
}
}

return ( pmove ->m_data );
}

//打印链表
template<typename Type>
void CircularList<Type>::Print()
{
ListNode<Type> *pmove = head ->m_pnext;
cout << "head";

while( pmove != head )
{
cout << " ----> " << ( pmove ->m_data );
pmove = pmove ->m_pnext;
}

cout << " ----> over " << endl << endl;
}

/* ListNode类 */
#pragma once

template<typename Type> class CircularList;
template<typename Type>
class ListNode
{
public:
ListNode(void);
~ListNode(void);
ListNode( const Type item, ListNode<Type> *next );


private:
friend class CircularList<Type>;
Type m_data;
ListNode *m_pnext;
};

#include "ListNode.h"

//构造函数
template<typename Type>
ListNode<Type>::ListNode(void)
{
m_pnext = NULL;
}

//析构函数
template<typename Type>
ListNode<Type>::~ListNode(void)
{
m_pnext = NULL;
}

//构造函数重载
template<typename Type>
ListNode<Type>::ListNode( const Type item, ListNode<Type> *next = NULL )
{
m_data = item;
m_pnext = next;
}

报错误:
错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall CircularList<int>::~CircularList<int>(void)" (??1?$CircularList@H@@QAE@XZ),该符号在函数 _main 中被引用
错误 2 error LNK2019: 无法解析的外部符号 "public: void __thiscall CircularList<int>::MakeEmpty(void)" (?MakeEmpty@?$CircularList@H@@QAEXXZ),该符号在函数 _main 中被引用
错误 3 error LNK2019: 无法解析的外部符号 "public: int __thiscall CircularList<int>::Get(int)" (?Get@?$CircularList@H@@QAEHH@Z),该符号在函数 _main 中被引用
错误 4 error LNK2019: 无法解析的外部符号 "public: bool __thiscall CircularList<int>::RemoveAll(int)" (?RemoveAll@?$CircularList@H@@QAE_NH@Z),该符号在函数 _main 中被引用
错误 5 error LNK2019: 无法解析的外部符号 "public: int __thiscall CircularList<int>::Remove(int)" (?Remove@?$CircularList@H@@QAEHH@Z),该符号在函数 _main 中被引用
错误 6 error LNK2019: 无法解析的外部符号 "public: void __thiscall CircularList<int>::Print(void)" (?Print@?$CircularList@H@@QAEXXZ),该符号在函数 _main 中被引用
错误 7 error LNK2019: 无法解析的外部符号 "public: int __thiscall CircularList<int>::Length(void)" (?Length@?$CircularList@H@@QAEHXZ),该符号在函数 _main 中被引用
错误 8 error LNK2019: 无法解析的外部符号 "public: bool __thiscall CircularList<int>::Insert(int,int)" (?Insert@?$CircularList@H@@QAE_NHH@Z),该符号在函数 _main 中被引用
错误 9 error LNK2019: 无法解析的外部符号 "public: __thiscall CircularList<int>::CircularList<int>(void)" (??0?$CircularList@H@@QAE@XZ),该符号在函数 _main 中被引用
错误 10 error LNK1120: 9 个无法解析的外部命令 D:\卢元平\数据结构\双向链表\Debug\双向链表.exe 双向链表
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
luciferisnotsatan 2011-09-28
  • 打赏
  • 举报
回复
CircularList<int> list;
for(int i=0;i<20;i++){
list.Insert(i*3,i);

bool CircularList<Type>::Insert( Type value, int n = 0 )


Type是一个int类型

ListNode<Type> *pnode = new ListNode<Type>(value);

class ListNode
{
public:
ListNode(void);
~ListNode(void);
ListNode( const Type item, ListNode<Type> *next );

List里没有一个只有一个Type参数的构造函数。
卢队长 2011-09-27
  • 打赏
  • 举报
回复
照二楼方法做后,出现的问题:
错误 1 error C2664: “ListNode<Type>::ListNode(const ListNode<Type> &)”: 不能将参数 1 从“int”转换为“const ListNode<Type> &”
这个怎么解决了????
ouyh12345 2011-09-27
  • 打赏
  • 举报
回复
支持2楼,模板类要放在一个文件里
luciferisnotsatan 2011-09-27
  • 打赏
  • 举报
回复
#include "CircularList.h"

//构造函数
template<typename Type>
CircularList<Type>::CircularList():head( new ListNode<Type>() )
{
head ->m_pnext = head;
}

全放头文件里去。模版不支持分离编译
卢队长 2011-09-27
  • 打赏
  • 举报
回复
但在主函数中把#include "CircularList.h"改为#include "CircularList.cpp"没有出错。出现错误错误 :
1 error C2664: “ListNode<Type>::ListNode(const ListNode<Type> &)”: 不能将参数 1 从“int”转换为“const ListNode<Type> &”

65,202

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧