问题遇到的现象和发生背景
刚学的C++,啥也不会。全靠网上自学。报了错也不知道到底是为什么。
问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;
//定义线性表类型List
template <class T>
class List{
void clear(); //清除表
bool insert(const int p, const T value); //插入表元
bool delete1(const int p); //删除表元
bool getPos(int &p, const T value); //查表元(返回位置)
bool getValue(const int p, T &value); //读表元(返回值)
bool setValue(const int p, const T value); //修改表元
int length(T &value); //求表长
void display(); //显示表
bool append(const T value); //追加表元
};
//定义顺序表类型arrList
template <class T>
class arrList : public List<T> {
private:
T* aList; //顺序表的指针
int maxSize; //顺序表的最大长度
int curLen; //顺序表实例的实际长度
int position; //当前读写位置
public:
//构造函数创建一个空顺序表
arrList(const int size) {
aList = new T[maxSize]; //开辟存储空间
maxSize = size;
curLen = position = 0;
}
//析构函数销毁顺序表
~arrList() {
delete[] aList; //释放存储空间
}
//清除表
bool clear() {
curLen = position = 0;
return true;
}
//显示表
bool display() {
if (curLen == 0) {cout << "空表!\n" ; return true; }
for (int i = 0; i < curLen; i++) cout << aList[i] << " ";
cout << endl;
return true;
}
//求表长
bool length(int& length) {
length=curLen;
return true;
}
//追加表元
bool append(const T value) {
if (curLen == maxSize) { //表满则自动扩大空间
T *pt1=aList;
aList=new T[maxSize+5];
for(int i=0; i<maxSize; i++) aList[i]=pt1[i];
maxSize=maxSize+5;
delete[] pt1;
}
aList[curLen] = value;
curLen++;
return true;
}
};
//插入表元
bool insert(const int p, const T value)
{
int i;
if (curLen >= maxSize)
{
cout << "The list is overflow" << endl;
return false;
}
if (p<0 || p>curLen)
{
cout << "Insertion point is illegal" << endl;
return false;
}
for (i = curLen; i > p;i--)
aList[i] = aList[i - 1];
aList[p] = value;
curLen++;
return true;}
//删除表元
bool delete1(const int p);
{int i;
if(curLen <= 0)
{ cout<<"无要素可删除"<<endl;
return false;}
if(p < 0 || p > curLen-1)
{cout<<"删除位置不合法"<<endl;
return false;}
for(i = p;i < curLen-1;i++)
{aList[i] = aList[i+1];
curLen--;
return true;}
}
//查找表元
bool getValue(const int p, T &value);
{if( curLen <= p ){
cout << "无元素可查找" << endl;
return false;
}
value = aList[ p ];
return ture;
break;
case 8:
if(curLen<=p)
cout<<"无元素可修改"<<endl;
return false;
aList[p]=value;
return true;}
//修改
bool setValue(const int p, const T value);
int i;
if (curLen>=maxSize)
{
cout << "修改失败!\n" << endl;
return false;
}
if(curLen < p || p<0)
{cout<<"无元素可修改"<<endl;
return false;}
aList[p] = value;
return true;
//定位
bool getPos(int &p, const T value);
for(int i = 0;i < curLen;i++)
{if(value == aList[i])
{p = i;
break;}
if(i < curLen)
return true;
else
return false;}
int main() {
arrList<int> AL(5); //定义及构造顺序表对象
int choice, p, value;
bool ok;
do {
cout<<"顺序表程序,请选择(0退出,1清除,2显示,3表长,4插入,5追加,6删除,7查找,8修改,9定位):";
cin>>choice;
switch (choice) {
case 0:
cout<<"再见!\n";
break;
case 1:
ok=AL.clear();
if(ok==false) cout<<"清除操作失败!\n";
else cout<<"清除操作成功!\n";
break;
case 2:
ok=AL.display();
if(ok==false) cout<<"显示操作失败!\n";
else cout<<"显示操作成功!\n";
break;
case 3:
ok=AL.length(p);
if(ok==false) cout<<"求表长操作失败!\n";
else cout<<"求表长操作成功!\n表长为:"<<p<<endl;
break;
case 4:
cout<<"插入位置,插入元素:";cin>>p>>value;
ok = AL.insert(p,value);
if(ok==false)
cout<<"插入失败!\n"endl;
else
cout<<"插入成功!\n"endl;
break;
case 5:
cout<<"元素:"; cin>>value;
ok=AL.append(value);
if(ok==true) cout<<"追加操作成功!\n";
else cout<<"追加操作失败!\n";
break;
case 6:
cout<<"删除位置:";cin>>p;
ok=AL.append(p);
if(ok==true)
cout<<"删除成功!\n"endl;
else
cout<<"删除失败!\n"endl;
break;
case 7:
cout<<"查找位置,查找元素:";cin>>p>>value;
ok = AL.setvalue(p,value);
if(ok==true)
cout<<"查找成功!\n"endl;
else
cout<<"查找失败!\n"endl;
break;
case 8:
cout<<"修改位置,修改内容";cin>>p>>value;
ok=AL.setvalue(p,value);
if(ok==true)
cout<<"修改成功!\n"endl;
else
cout<<"修改失败!\n"endl;
break;
case 9:
cout<<"位置,元素";cin>>p>>value;
ok=Al.append(value);
if(ok==true)
cout<<"定位成功!\n"endl;
else
cout<<"定位失败!\n"endl;
break;
default:
cout<<"选择错误"<<;
}
} while (choice != 0);
}
运行结果及报错内容
一堆报错啊,我也不知道到底错哪了555。
C:\Users\86147\Desktop\C,C++\顺序表.cpp(86) : error C2146: syntax error : missing ',' before identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(86) : error C2061: syntax error : identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(89) : error C2065: 'curLen' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(89) : error C2065: 'maxSize' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(100) : error C2065: 'aList' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(100) : error C2109: subscript requires array or pointer type
C:\Users\86147\Desktop\C,C++\顺序表.cpp(100) : error C2109: subscript requires array or pointer type
C:\Users\86147\Desktop\C,C++\顺序表.cpp(100) : error C2106: '=' : left operand must be l-value
C:\Users\86147\Desktop\C,C++\顺序表.cpp(101) : error C2109: subscript requires array or pointer type
C:\Users\86147\Desktop\C,C++\顺序表.cpp(101) : error C2065: 'value' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(101) : error C2106: '=' : left operand must be l-value
C:\Users\86147\Desktop\C,C++\顺序表.cpp(106) : error C2447: missing function header (old-style formal list?)
C:\Users\86147\Desktop\C,C++\顺序表.cpp(122) : error C2061: syntax error : identifier 'T'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(123) : error C2447: missing function header (old-style formal list?)
C:\Users\86147\Desktop\C,C++\顺序表.cpp(138) : error C2146: syntax error : missing ',' before identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(138) : error C2061: syntax error : identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(140) : error C2143: syntax error : missing ';' before 'if'
C:\Users\86147\Desktop\C,C++\顺序.cpp(141) : error C2143: syntax error : missing ';' before '{'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(141) : error C2447: missing function header (old-style formal list?)
C:\Users\86147\Desktop\C,C++\顺序表.cpp(145) : error C2143: syntax error : missing ';' before 'if'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(146) : error C2143: syntax error : missing ';' before '{'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(146) : error C2447: missing function header (old-style formal list?)
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2065: 'p' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2057: expected constant expression
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2466: cannot allocate an array of constant size 0
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2501: 'aList' : missing storage-class or type specifiers
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2040: 'aList' : 'int []' differs in levels of indirection from 'int'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(148) : error C2440: 'initializing' : cannot convert from 'int' to 'int []'
There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Users\86147\Desktop\C,C++\顺序表.cpp(149) : error C2143: syntax error : missing ';' before 'return'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(152) : error C2146: syntax error : missing ',' before identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(152) : error C2061: syntax error : identifier 'value'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ';' before 'for'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ')' before ';'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ';' before '<'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2501: 'i' : missing storage-class or type specifiers
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2086: 'i' : redefinition
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ';' before '<'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ';' before '++'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2501: 'i' : missing storage-class or type specifiers
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2086: 'i' : redefinition
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2143: syntax error : missing ';' before '++'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(154) : error C2059: syntax error : ')'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(155) : error C2143: syntax error : missing ';' before '{'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(155) : error C2447: missing function header (old-style formal list?)
C:\Users\86147\Desktop\C,C++\顺序表.cpp(191) : error C2248: 'insert' : cannot access private member declared in class 'List'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(17) : see declaration of 'insert'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(193) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(193) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(194) : error C2181: illegal else without matching if
C:\Users\86147\Desktop\C,C++\顺序表.cpp(195) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(195) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(207) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(207) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(208) : error C2181: illegal else without matching if
C:\Users\86147\Desktop\C,C++\顺序表.cpp(209) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(209) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(214) : error C2039: 'setvalue' : is not a member of 'arrList'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(216) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(216) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(217) : error C2181: illegal else without matching if
C:\Users\86147\Desktop\C,C++\顺序表.cpp(218) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(218) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(223) : error C2039: 'setvalue' : is not a member of 'arrList'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(225) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(225) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(226) : error C2181: illegal else without matching if
C:\Users\86147\Desktop\C,C++\顺序表.cpp(227) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(227) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(232) : error C2065: 'Al' : undeclared identifier
C:\Users\86147\Desktop\C,C++\顺序表.cpp(232) : error C2228: left of '.append' must have class/struct/union type
C:\Users\86147\Desktop\C,C++\顺序表.cpp(234) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(234) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(235) : error C2181: illegal else without matching if
C:\Users\86147\Desktop\C,C++\顺序表.cpp(236) : error C2146: syntax error : missing ';' before identifier 'endl'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(236) : warning C4551: function call missing argument list
C:\Users\86147\Desktop\C,C++\顺序表.cpp(241) : error C2059: syntax error : ';'
C:\Users\86147\Desktop\C,C++\顺序表.cpp(244) : warning C4508: 'main' : function should return a value; 'void' return type assumed
我的解答思路和尝试过的方法
我想要达到的结果
告诉我他为什么错了,然后让他能成功运行