刚学c++,以前写过一个vector对象数组,对vector用法不熟,现在想给他加一个删除和修改的操作,对照网上一个修改但是还是报错,不知怎么改了,求指教
问题在红色标注地方
# include<iostream>
# include <string>
# include <vector>
using namespace std;
class Student
{public:
Student()
{number=0;
score=0;
num++;
}
void setdata(double a,char m[],double s);
void disp();
static void avg(void);
double geta(void){return number;}
private:
double number,score;
char name[20];
static double sum,num;
};
double Student::sum=0;
double Student::num=0;
void Student::setdata(double number1,char m[],double score1)
{ number=number1;
strcpy(name,m);
score=score1;
sum=sum+score;
}
void Student::disp()
{
cout<<number<<" "<<name<<" "<<score;
}
void Student::avg()
{
cout<<"平均成绩是: "<<sum/num;
}
int main()
{
int i=0;
double number=1,score=0;
char name[20];
cout<<"请输入学生的学号(按0结束输入),姓名,成绩:"<<endl;
vector<Student>Stu;
for(i=0;number!=0;i++)
{
cin>>number;
if(number==0) break;
cin>>name>>score;
Student stu;
stu.setdata(number,name,score);
Stu.push_back(stu);
}
for(i=0;i<Stu.size();i++)
{
Stu[i].disp();
cout<<endl;
}
cout<<endl;
cout<<"请输入要删除学号"<<endl;
double mn;
cin>>mn;
for(int e=Stu.size()-1;e>=0;e--)
if(Stu[i].geta() ==mn)
{delete (Stu[e]);Stu.erase(Stu.begin()+e);}
Student::avg();
cout<<endl;
return 0;
}
------解决方案--------------------------------------------------------
#include<iostream>
# include <string>
# include <vector>
using namespace std;
class Student
{
public:
Student()
{number=0;
score=0;
num++;
}
void setdata(double a,char m[],double s);
void disp();
static void avg(void);
double geta(void){return number;}
private:
double number,score;
char name[20];
static double sum,num;
};
double Student::sum=0;
double Student::num=0;
void Student::setdata(double number1,char m[],double score1)
{
number=number1;
strcpy(name,m);
score=score1;
sum=sum+score;
}
void Student::disp()
{
cout<<number<<" "<<name<<" "<<score;
}
void Student::avg()