qq_36204385 于 2017.09.19 11:58 提问

#include
using namespace std;
template
class SqList
{
private:
T *elem;
int length;
int listsize;
public:
SqList(int m);
~SqList();
void CreateList(int m);
void Insert(int i, T e);
T Delete(int i);
};
template
SqList::SqList(int m)
{
elem = new T[m];
length = 0;
listsize = m;
}
template
SqList::~SqList()
{
delete[]elem;
length = 0;
listsize = 0;
}
template
void SqList::CreateList(int n)
{
if (n > listsize)throw"参数非法";
cout << "请依次输入" << n << "个元素值：" << endl;
for (int i = 1; i <= n; i++)
cin >> elem[i - 1];
length = n;
}
template
void SqList::Insert(int i, T e)
{
if (length >= listsize)cout << "上溢" << endl;
if (ilength + 1)cout << "插入位置异常" << endl;
for (int j = length; j >= i; j++)
elem[j] = elem[j - 1];
elem[i - 1] = e;
length++;
}
template
T SqList::Delete(int i)
{
T x;
if (length == 0） cout << "删除位置异常" << endl;
x = elem[i - 1];
for (int j = i; j < length; j++)
elem[j - 1] = elem[j];
length--;
return x;
}
typedef int T;
int main()
{
int i;
T e;
SqListL(5);
int choice;
do
{
cout << "1-创建顺序表\n";
cout << "2-在顺序表的第i位置插入元素\n";
cout << "3-删除顺序表中第i个位置的元素\n";
cout << "4-显示表\n";
cout << "5-退出\n";
cout << "Enter choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入要创建的顺序表中的个数：";
cin >> i;
L.CreateList(i);
cout << endl;
break;
case 2:
cout << "请输入插入的位置：";
cin >> i;
cout << endl;
cout << "请输入插入元素的值：";
cin >> e;
cout << endl;
L.Insert(i,e);
break;

``````    case 3:
cout << "请输入删除的位置：";
cin >> i;
cout << endl;
e = L.Delete(i);
cout << "被删除的元素为：" << e << endl;
break;
case 4:
break;
default:
cout << "Invalid choice !\n";
break;
}
} while (choice != 4);
return 0;
``````

}

1个回答

caozhy      2017.09.19 13:30

`````` 你的后括号是全角的），改成半角 ) 才行，这种错误有好几个

< = 这里不能有空格，应该是 <=

elem = [j] = elem[j - 1]; 这个你上次就错了，说了你还不改

``````
caozhy 回复Joker_Jay: 把你现在的代码贴出来
5 个月之前 回复
qq_36204385 回复caozhy: 是的，那个括弧和elem = [j]这个地方
5 个月之前 回复
caozhy 回复Joker_Jay: 说了有好多处，你都改了么
5 个月之前 回复
qq_36204385 我改了还是会出现这个错误啊，我的天。
5 个月之前 回复