c++实现顺序表,该怎么处理
c++实现顺序表
RT,我先简单的写了一些,头文件如下:
main:
出现如下错误:
rror LNK2019: 无法解析的外部符号 "class Sqlist<int> __cdecl RandCreat(void)" (?RandCreat@@YA?AV?$Sqlist@H@@XZ),该符号在函数 _main 中被引用
1>47.obj : error LNK2019: 无法解析的外部符号 "void __cdecl Display(class Sqlist<int>)" (?Display@@YAXV?$Sqlist@H@@@Z),该符号在函数 _main 中被引用
1>D:\c++primer\编程之路\Debug\编程之路.exe : fatal error LNK1120: 2 个无法解析的外部命令
求帮助
------解决思路----------------------
看法同楼上
模板没特化啊
RT,我先简单的写了一些,头文件如下:
#ifndef SQLIST_H_
#define SQLIST_H_
#define LIST_MAX_SIZE 100
#define LISTINCREMENT 10
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
template <class ElemType>
class Sqlist
{
private:
ElemType *elem; //顺序表动态存储空间首地址
int listSize; //当前存储空间大小
int n; //当前元素个数
public:
Sqlist();
Sqlist(const Sqlist<ElemType> & otherL );
virtual ~Sqlist();
Sqlist<ElemType> operator = (Sqlist<ElemType> rightL);
friend Sqlist<ElemType> RandCreat(); //随机生成一个顺序表
friend void Display ( Sqlist<ElemType> PG );//输出顺序表
};
template <class ElemType> //构造函数
Sqlist<ElemType>::Sqlist()
{
elem = new ElemType[LIST_MAX_SIZE];
// assert(elem!=0); //有啥用?
listSize=LIST_MAX_SIZE;
n=0;
}
template <class ElemType> //拷贝初始化构造函数
Sqlist<ElemType>::Sqlist( const Sqlist<ElemType>& otherL)
{
listSize=otherL.listSize;
elem = new ElemType[listSize];
n=otherL.n;
for( int i=0 ; i!=n; i++ )
*(elem+i)=*(otherL.elem+i);
}
template <class ElemType> //析构函数
Sqlist<ElemType>::~Sqlist()
{
delete [] elem;
n=0;
listSize=0;
}
template<class ElemType> //重载=运算符
Sqlist<ElemType> Sqlist<ElemType>:: operator = ( Sqlist<ElemType>rightL )
{
listSize=rightL.listSize;
elem= new ElemType[ listSize ];
n= rightL.n;
for (int i=0; i != n; i++ )
{
elem[i]=rightL.elem[i];
}
return *this;
}
template<class ElemType> //随机生产一个顺序表
Sqlist<ElemType> RandCreat()
{
Sqlist<ElemType> pg;
pg.listSize= rand() % 10 +1;
pg.elem = new ElemType[ pg. listSize ];
n= rand()% (pg.listSize-1) ;
srand( (unsigned) time (NULL) );
for( int i=0; i!=n; i++ )
{
pg.elem[i] = rand() % 99 + 1;
}
return pg;
}
template <class ElemType>
void Display( Sqlist<ElemType> PG ) //输出顺序表
{
using std::cout;
using std::endl;
for ( int i=0; i<PG.n; i++ )
cout<<i+1<<setw(5);
cout<<endl;
for( int j=0; j<PG.n; j++ )
cout<<PG.elem[j]<<setw(5);
cout<<endl;
}
#endif
main:
#include <iostream>
#include "Sqlist.h"
int main()
{
using std::cout;
using std::endl;
Sqlist<int> A;
A=RandCreat();
cout<<"随机生成的顺序表为:"<<endl;
Display(A);
system("pause");
return 0;
}
出现如下错误:
rror LNK2019: 无法解析的外部符号 "class Sqlist<int> __cdecl RandCreat(void)" (?RandCreat@@YA?AV?$Sqlist@H@@XZ),该符号在函数 _main 中被引用
1>47.obj : error LNK2019: 无法解析的外部符号 "void __cdecl Display(class Sqlist<int>)" (?Display@@YAXV?$Sqlist@H@@@Z),该符号在函数 _main 中被引用
1>D:\c++primer\编程之路\Debug\编程之路.exe : fatal error LNK1120: 2 个无法解析的外部命令
求帮助
------解决思路----------------------
看法同楼上
模板没特化啊