//头文件h1.h
#include<iostream>
using namespace std;
#define DEFAULT_SIZE 100;
template < class ElemType >
class Seqlist{
private:
int length; //线性表长度
int maxlength; //最大长度
ElemType *elems; //元素存储空间首地址
public:
Seqlist(int SIZE = DEFAULT_SIZE); //构造函数,构造一个空链表;
Seqlist(ElemType v[], int n, int SIZE = DEFAULT_SIZE); //输入数组直接构造函数,
virtual ~Seqlist(); //析构函数
int GetLength() const; //得到长度,声明后加const 表明函数不能对其中的数据做操作
bool IsEmpty() const; //???函数的定义为什么非要加const;
void Clear(); //清空线性表
void Traverse(void (*visit)(const ElemType &)) const; //用函数形参作遍历;
int LocateElem(const ElemType &e); //定位元素
bool GetElem( ElemType &e, int n); //获取制定元素;
bool SetElem(const ElemType &e, int n); //修改制定元素
bool DeletElem(ElemType &e, int n); //删除制定元素
bool AddElem(const ElemType &e, int n); //添加制定元素
bool AddElem(const ElemType &e);
Seqlist(const Seqlist<ElemType> &sa); //复制构造函数,方便赋值
Seqlist<ElemType> operator =(const SeqList<ElemType> &sa);
};
//在cpp里定义类成员函数
#include<iostream>
#include"h1.h"
using namespace std;
template<class ElemType>
Seqlist<ElemType>::Seqlist(int SIZE){
elems = new ElemType[SIZE];
assert(elems); //存储空间分配失败,程序终止;
maxlength = SIZE;
length = 0;
}
template<class ElemType>
Seqlist<ElemType>::Seqlist(ElemType v[], int n, int SIZE){
elems = new ElemType[SIZE]; //新建一个SIZE长度的数组,指针返回给elems;
assert(elems);
maxlength = SIZE;
length = n;
for (int i = 0; i < n; i++){
elems[i] = v[i];
}
}
template<class ElemType>
Seqlist<ElemType>::~Seqlist() {
delete[] elems; //用delet []函数释放一个数组内存;
}
template<class ElemType>
int Seqlist<ElemType>::GetLength() const{
return length;
}
template<class ElemType>
bool Seqlist<ElemType>::IsEmpty() const{
if (length !=0 ) {
return true;
}
else
{
return false;
}
}
template<class ElemType>
void Seqlist<ElemType>::Clear(){
length = 0;
}
template<class ElemType>
void Seqlist<ElemType>::Traverse(void (*visit)(const ElemType &)) const
{
for (int i = 0; i < length; i++)
{
(*visit)(elems[i]);
}
}
template<class ElemType>
int Seqlist<ElemType>::LocateElem(const ElemType &e)
{
int i = 0;
while (i < length && elems[i] != e)
i++;
return i < length ? i++ : 0;
}
template<class ElemType>
bool Seqlist<ElemType>::GetElem(ElemType &e, int n){
if (n<1 || n>length){
return false;
}
else
{
e = elems[n - 1];
return true;
}
}
template<class ElemType>
bool Seqlist<ElemType>::SetElem(const ElemType &e, int n){
if (n<1 || n>length){
return false;
}
else
{
elems[n - 1]=e;
return true;
}
}
template<class ElemType>
bool Seqlist<ElemType>::DeletElem(ElemType &e, int n){
if (n<1 || n>length){
return false;
}
else
{
e = elems[n - 1];
for (int j = n; j < length; j++)
{
elems[j] = elems[j + 1];
}
length--;
return true;
}
}
template<class ElemType>
bool Seqlist<ElemType>::AddElem(const ElemType &e, int n){
if (n<1 || n>length){
return false;
}
else
{
for (int j = length; j >n-1; j--)
{
elems[j] = elems[j - 1];
}
elems[n - 1] = e;
length++;
return true;
}
}
template<class ElemType>
bool Seqlist<ElemType>::AddElem(const ElemType &e){
elems[length] = e;
length++;
}
//main函数:
#include<iostream>
#include"h1.h"
using namespace std;
void main(){
int x[] = { 1, 2, 3, 4, 5, 6, 7 };
Seqlist<int> S(x,7);
S.IsEmpty() ? cout << "EMPTY" : cout << "UNEMPTY";
}
1>------ 已启动生成: 项目: 线性表, 配置: Debug Win32 ------
1> 源.cpp
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(13): error C2143: 语法错误 : 缺少“)”(在“;”的前面)
1> c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(29): 参见对正在编译的类 模板 实例化“Seqlist<ElemType>”的引用
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(13): error C2059: 语法错误:“)”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(14): error C2143: 语法错误 : 缺少“)”(在“;”的前面)
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(14): error C2059: 语法错误:“)”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(19): error C2062: 意外的类型“void”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(27): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
1> cpp1.cpp
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(13): error C2143: 语法错误 : 缺少“)”(在“;”的前面)
1> c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(29): 参见对正在编译的类 模板 实例化“Seqlist<ElemType>”的引用
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(13): error C2059: 语法错误:“)”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(14): error C2143: 语法错误 : 缺少“)”(在“;”的前面)
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(14): error C2059: 语法错误:“)”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(19): error C2062: 意外的类型“void”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(27): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\h1.h(27): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2059: 语法错误:“)”
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2065: “visit”: 未声明的标识符
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2146: 语法错误: 缺少“)”(在标识符“(const”的前面)
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2182: “Traverse”: 非法使用“void”类型
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2470: “Seqlist<ElemType>::Traverse”: 看起来像函数定义,但没有参数列表;跳过明显的函数体
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(54): error C2072: “Seqlist<ElemType>::Traverse”: 函数的初始化
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(64): error C2065: “ElemType”: 未声明的标识符
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(64): error C2923: “Seqlist”: 对于参数“ElemType”,“ElemType”不是有效的 模板 类型变量
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(65): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
1>c:\users\youzi\documents\visual studio 2013\projects\线性表\线性表\cpp1.cpp(65): error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
1> 正在生成代码...
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
2 个解决方案
#1
#define DEFAULT_SIZE 100;
多了个分号,去掉即可
void Traverse(void (*visit)(const ElemType &)) const;
中
void (*visit)(const ElemType &)中间使用了中文的(),改为英文()
Seqlist<ElemType> operator =(const SeqList<ElemType> &sa);
参数中的类型大小写错了
Seq List<ElemType>
为
Seqlist<ElemType>
多了个分号,去掉即可
void Traverse(void (*visit)(const ElemType &)) const;
中
void (*visit)(const ElemType &)中间使用了中文的(),改为英文()
Seqlist<ElemType> operator =(const SeqList<ElemType> &sa);
参数中的类型大小写错了
Seq List<ElemType>
为
Seqlist<ElemType>
#2
已弄明白,谢谢指点!
#1
#define DEFAULT_SIZE 100;
多了个分号,去掉即可
void Traverse(void (*visit)(const ElemType &)) const;
中
void (*visit)(const ElemType &)中间使用了中文的(),改为英文()
Seqlist<ElemType> operator =(const SeqList<ElemType> &sa);
参数中的类型大小写错了
Seq List<ElemType>
为
Seqlist<ElemType>
多了个分号,去掉即可
void Traverse(void (*visit)(const ElemType &)) const;
中
void (*visit)(const ElemType &)中间使用了中文的(),改为英文()
Seqlist<ElemType> operator =(const SeqList<ElemType> &sa);
参数中的类型大小写错了
Seq List<ElemType>
为
Seqlist<ElemType>
#2
已弄明白,谢谢指点!