首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求解这是什么有关问题哟

2012-06-13 
求解这是什么问题哟!#include iostream.h#include string.hclass Student{char Num[11]char *Namech

求解这是什么问题哟!
#include <iostream.h>
#include <string.h>
class Student
{
char Num[11];
char *Name;
char Sex;
double Score[3];
public:
Student(char *nump,char *namep,char sexp,double *scorep)
{
if(nump)
strcpy(Num,nump);
else
strcpy(Num," ");
if(namep)
{
Name=new char[strlen(namep)+1];
strcpy(Name,namep);
}
else
Name=0;
Sex=sexp;
if(scorep)
{
for(int i=0;i<3;i++)
Score[i]=scorep[i];
}
else
*Score=NULL;
cout<<"Constructor Called 1 !\n";
}
Student(Student &stud)
{
if(stud.Num)
strcpy(Num,stud.Num);
else
strcpy(Num," ");
if(stud.Name)
{
Name=new char[strlen[stud.Name]+1];
strcpy(Name,stud.Name);
}
else
Name=0;
Sex=stud.Sex;
if(stud.Score)
{
for(int i=0;i<3;i++)
Score[i]=stud.Score[i];
}
else
*Score=NULL;
cout<<"Constructor Called 2 !\n";
}
~Student()
{
if(Name) delete [] Name;
cout<<"Destructor Called!\n";
}
void show()
{
cout<<"学号为:"<<Num<<endl;
cout<<"姓名为:"<<Name<<endl;
cout<<"性别为:"<<Sex<<endl;
cout<<"各科成绩如下:";
for(int i=0;i<3;i++)
cout<<Score[i]<<'\t';
cout<<endl;
}
};
void main()
{
int b[]={100,99,98};
Student a={"12345","XXX","男",b};
a.show();
Student c=a;
c.show();
}


不能编译通过







[解决办法]

C/C++ code
#include <iostream>#include <string>using namespace std;class Student{private:    char Num[11];    char *Name;    char Sex[5];//将char sex改为char sex[5]    double Score[3];public:    Student(char *nump,char *namep,char *sexp,double *scorep)//将char sexp改为char *sexp    {        if(nump)            strcpy(Num,nump);        else            strcpy(Num," ");        if(namep)        {            Name=new char[strlen(namep)+1];            strcpy(Name,namep);        }        else            Name=0;        //Sex=sexp;        strcpy(Sex,sexp);        if(scorep)        {            for(int i=0;i<3;i++)                Score[i]=scorep[i];        }        else            *Score=NULL;        cout<<"Constructor Called 1 !\n";    }Student(Student &stud){    if(stud.Num)        strcpy(Num,stud.Num);    else        strcpy(Num," ");    if(stud.Name)    {        //Name=new char[strlen[stud.Name]+1];        Name=new char[strlen(stud.Name)+1];        strcpy(Name,stud.Name);    }    else        Name=0;    //Sex=stud.Sex;    strcpy(Sex,stud.Sex);if(stud.Score){for(int i=0;i<3;i++)Score[i]=stud.Score[i];}else*Score=NULL;cout<<"Constructor Called 2 !\n";}~Student(){if(Name) delete [] Name;cout<<"Destructor Called!\n";}void show(){cout<<"学号为:"<<Num<<endl;cout<<"姓名为:"<<Name<<endl;cout<<"性别为:"<<Sex<<endl;cout<<"各科成绩如下:";for(int i=0;i<3;i++)cout<<Score[i]<<'\t';cout<<endl;}};void main(){    //int b[]={100,99,98};    double b[]={100,99,98};//int 改为double    //Student a={"12345","XXX","男",b};    Student a("12345","XXX","男",b);    a.show();    Student c=a;    c.show();}//太多问题了,我只是让他编译通过了,还有很多问题存在的,楼主再去琢磨一下吧 

热点排行