就是这个“输入任意数量”,我想定义一个指针数组指向的是对象,不是对象的内容,可是怎么试都错,不知道我表达清楚自己的想法没,恳请大家指导下我。
void main()
{
int k=0;
CStudent *stu =new CStudent;
*stu=CStudent("1","张三",90,89,80,72);
*(stu+1)=CStudent("2","李三",70,79,60,72); // (1)
*(stu+2)=CStudent("3","王三",59,79,60,72); // (2)
}
其中:
class CStudent
{
public:
char *ID,*name;
float a,b,c,d,ave;
public:
CStudent(){};
CStudent(char*,char*,float,float,float,float);
~CStudent(){};
void display(){cout<<name<<" "<<ave<<"\n";};
};
第(1)步和第(2)步报错,我不知道如何动态分配一个指针指向的是对象而不是对象的内容。
8 个解决方案
#1
动态数组就可以解决,使用STL中的向量
#2
CStudent *stu =new CStudent[??];
建议用CArray<CStudent ,CStudent > stu;
stu.Add
public:
char *ID,*name;
换成string
建议用CArray<CStudent ,CStudent > stu;
stu.Add
public:
char *ID,*name;
换成string
#3
或者用std::vector存任意数量学生的...
#4
CStudent *stu =new CStudent;
*stu=CStudent("1","张三",90,89,80,72);
*(stu+1)=CStudent("2","李三",70,79,60,72); // (1)
*(stu+2)=CStudent("3","王三",59,79,60,72); // (2)
=>
CStudent** stu = new CStudent*[2];
*stu = new CStudent();
*(stu+1) = new CStudent();
*stu=CStudent("1","张三",90,89,80,72);
*(stu+1)=CStudent("2","李三",70,79,60,72); // (1)
*(stu+2)=CStudent("3","王三",59,79,60,72); // (2)
=>
CStudent** stu = new CStudent*[2];
*stu = new CStudent();
*(stu+1) = new CStudent();
#5
新分配的动态空间需要指定大小吧,用完需要释放!
#6
简单的话,用CArray等可动态扩展的数组,不停的push_back()插入你的CStudent对象
#7
class CStudent
{
public:
char *ID,*name;
float a,b,c,d,ave;
public:
CStudent(){};
CStudent(char* chID,char* chName,float fa,float fb,float fc,float fd){ ID=chID; name=chName; a=fa; b=fb; c=fc; d=fd;};
~CStudent(){};
//void display(){cout<<name<<" "<<ave<<"\n";};
};
int main(char arg,char *argv[])
{
int main(char arg,char *argv[])
{
CStudent *pStu = new CStudent[100];
pStu[0] = CStudent("0000001","张三",60,70,80,90);
pStu[1] = CStudent("0000002","李四",60,70,80,90);
pStu[2] = CStudent("0000003","王麻子",60,70,80,90);
....
....
pStu[99]= CStudent("0000099","王麻子",60,70,80,90);
....自己delete去
}
{
public:
char *ID,*name;
float a,b,c,d,ave;
public:
CStudent(){};
CStudent(char* chID,char* chName,float fa,float fb,float fc,float fd){ ID=chID; name=chName; a=fa; b=fb; c=fc; d=fd;};
~CStudent(){};
//void display(){cout<<name<<" "<<ave<<"\n";};
};
int main(char arg,char *argv[])
{
int main(char arg,char *argv[])
{
CStudent *pStu = new CStudent[100];
pStu[0] = CStudent("0000001","张三",60,70,80,90);
pStu[1] = CStudent("0000002","李四",60,70,80,90);
pStu[2] = CStudent("0000003","王麻子",60,70,80,90);
....
....
pStu[99]= CStudent("0000099","王麻子",60,70,80,90);
....自己delete去
}
#8
delete []pStu;
#1
动态数组就可以解决,使用STL中的向量
#2
CStudent *stu =new CStudent[??];
建议用CArray<CStudent ,CStudent > stu;
stu.Add
public:
char *ID,*name;
换成string
建议用CArray<CStudent ,CStudent > stu;
stu.Add
public:
char *ID,*name;
换成string
#3
或者用std::vector存任意数量学生的...
#4
CStudent *stu =new CStudent;
*stu=CStudent("1","张三",90,89,80,72);
*(stu+1)=CStudent("2","李三",70,79,60,72); // (1)
*(stu+2)=CStudent("3","王三",59,79,60,72); // (2)
=>
CStudent** stu = new CStudent*[2];
*stu = new CStudent();
*(stu+1) = new CStudent();
*stu=CStudent("1","张三",90,89,80,72);
*(stu+1)=CStudent("2","李三",70,79,60,72); // (1)
*(stu+2)=CStudent("3","王三",59,79,60,72); // (2)
=>
CStudent** stu = new CStudent*[2];
*stu = new CStudent();
*(stu+1) = new CStudent();
#5
新分配的动态空间需要指定大小吧,用完需要释放!
#6
简单的话,用CArray等可动态扩展的数组,不停的push_back()插入你的CStudent对象
#7
class CStudent
{
public:
char *ID,*name;
float a,b,c,d,ave;
public:
CStudent(){};
CStudent(char* chID,char* chName,float fa,float fb,float fc,float fd){ ID=chID; name=chName; a=fa; b=fb; c=fc; d=fd;};
~CStudent(){};
//void display(){cout<<name<<" "<<ave<<"\n";};
};
int main(char arg,char *argv[])
{
int main(char arg,char *argv[])
{
CStudent *pStu = new CStudent[100];
pStu[0] = CStudent("0000001","张三",60,70,80,90);
pStu[1] = CStudent("0000002","李四",60,70,80,90);
pStu[2] = CStudent("0000003","王麻子",60,70,80,90);
....
....
pStu[99]= CStudent("0000099","王麻子",60,70,80,90);
....自己delete去
}
{
public:
char *ID,*name;
float a,b,c,d,ave;
public:
CStudent(){};
CStudent(char* chID,char* chName,float fa,float fb,float fc,float fd){ ID=chID; name=chName; a=fa; b=fb; c=fc; d=fd;};
~CStudent(){};
//void display(){cout<<name<<" "<<ave<<"\n";};
};
int main(char arg,char *argv[])
{
int main(char arg,char *argv[])
{
CStudent *pStu = new CStudent[100];
pStu[0] = CStudent("0000001","张三",60,70,80,90);
pStu[1] = CStudent("0000002","李四",60,70,80,90);
pStu[2] = CStudent("0000003","王麻子",60,70,80,90);
....
....
pStu[99]= CStudent("0000099","王麻子",60,70,80,90);
....自己delete去
}
#8
delete []pStu;