为什么会生成错误?24行有个警告,但是我代码都是照着书上敲的,是哪里的代码出了问题?
#include<iostream>#include<iomanip>
using namespace std;
const int Maxsize = 100;
template<typename T>
class Seqlist
{
public:
Seqlist() { length = 0; }
Seqlist(T a[], int n);
~Seqlist();
int Length();
T Get(int i);
int Locate(T x);
void Insert(int i, T x);
T Delete(int i);
int Empty();
void Printlist();
private:
T data[Maxsize];
int length;
};
template<typename T>
Seqlist<T>::Seqlist(T a[], int n)
{
if (n > Maxsize)throw"参数非法";
for (int i = 0; i < n; i++)
{
data[i] = a[i];
}
length = n;
}
template<typename T>
int Seqlist<T>::Length()
{
return length;
}
template<typename T>
T Seqlist<T>::Get(int i)
{
if (i<1 || i>length)throw"查找位置非法";
else
return data[i - 1];
}
template<typename T>
int Seqlist<T>::Locate(T x)
{
for (int i = 0; i < length; i++)
{
if (data[i] == x)return i - 1;
}
return 0;
}
template<typename T>
void Seqlist<T>::Insert(int i, T x)
{
if (length == Maxsize)throw"上溢";
if (i<1 || i>length+1)throw"插入位置错误";
for (int j = length; j >= i; j--)
{
data[j] = data[j - 1];
}
data[i - 1] = x;
length++;
}
template<typename T>
T Seqlist<T>::Delete(int i)
{
if (length == 0)throw"下溢";
if (i<1 || i>length)throw"删除位置非法";
T x = data[i - 1];
for (int j = i ; j < length ; j++)
{
data[j-1] = data[j];
}
length--;
return x;
}
/*template<typename T>
int Seqlist<T>::Empty()
{
if (length == 0)return 0;
else
return 1;
}*/
template<typename T>
void Seqlist<T>::Printlist()
{
for (int i = 0; i < length; i++)
{
cout << data[i] << "\t";
}
cout << endl;
}
int main()
{
int r[5] = { 1,2,3,4,5 };
int i, x;
Seqlist<int> L{ r,5 };
cout << "当前线性表长度为:";
L.Printlist();
try {
L.Insert(2, 8);
cout << "执行插入操作后数据为:";
L.Printlist();
}
catch (char* str) { cout << str << endl; }
cout << "当前线性表长度为:" << L.Length() << endl;
cout << "请输入查找的元素值:";
cin >> x;
i = L.Locate(x);
if (i == 0)cout << "查找失败" << endl;
else cout << "元素位置为:" << i << endl;
try
{
cout << "请输入查找元素位置:";
cin >> i;
cout << "元素值是" << L.Get(i) << endl;
}
catch (char* str) { cout << str << endl; }
try
{
cout << "请输入要删除的元素序号";
cin >> i;
x = L.Delete(i);
cout << "删除的元素是" << x << endl;
cout << "删除后的数据为" << endl;
L.Printlist();
}
catch (char* str) { cout << str << endl; }
return 0;
}
回答
~Seqlist()
析构函数定义了,但是没有实现。是不是少了代码。
以上是 为什么会生成错误?24行有个警告,但是我代码都是照着书上敲的,是哪里的代码出了问题? 的全部内容, 来源链接: utcz.com/a/57498.html