c++实现顺序表

鹏割 2015-01-05 09:07:44
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 个无法解析的外部命令


求帮助
...全文
411 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_dragon_fly 2015-01-06
  • 打赏
  • 举报
回复
定义改一下

    template<class T>
    friend Sqlist<T> RandCreat(); //随机生成一个顺序表
    template<class T>
    friend void Display ( Sqlist<T> PG );//输出顺序表
能const reference就尽量用, display函数不如直接提供 operator<<
乔巴好萌 2015-01-05
  • 打赏
  • 举报
回复
看法同楼上 模板没特化啊
encoderlee 版主 2015-01-05
  • 打赏
  • 举报
回复

#define LIST_MAX_SIZE 100
#define LISTINCREMENT 10
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;


template <class ElemType>
class Sqlist
{
public:
	ElemType *elem; //顺序表动态存储空间首地址
	int listSize; //当前存储空间大小
	int n; //当前元素个数

public:
	Sqlist();
	Sqlist(const Sqlist<ElemType> & otherL );
	virtual ~Sqlist();
	Sqlist<ElemType> operator = (Sqlist<ElemType> rightL);
	template <class ElemType>
	friend Sqlist<ElemType> RandCreat(); //随机生成一个顺序表
	template <class ElemType>
	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 ];
	int 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;
}

int main()
{
	Sqlist<int> A;
	A=RandCreat<int>();
	cout<<"随机生成的顺序表为:"<<endl;
	Display(A);
	system("pause");
	return 0;
}
问题一: A=RandCreat(); 仅凭返回值无法推导出模板,改法: A=RandCreat<int>(); 问题二: 友元函数并不是类的一部分,友元函数其实就是个普通的全局函数,其声明成函数模板时要这样来 template <class ElemType> friend Sqlist<ElemType> RandCreat(); template <class ElemType> friend void Display ( Sqlist<ElemType> PG ); 并且友元函数会破坏程序的封装性,这里的RandCreat实现为类的静态成员函数,Display实现为类的成员函数会更好些。 问题三: RandCreat中的变量n没有定义 问题四: 代码逻辑上可能存在问题
Evankaka 版主 2015-01-05
  • 打赏
  • 举报
回复
// assert(elem!=0); //有啥用 断言,当elem为空时报错,输出报错信息

64,654

社区成员

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

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