问题在 Student 类:
第一、Student 类的析构函数定义写错。
第二、需要定义 Student 类的默认构造函数。
参考以下代码:
class Student
{
public:
Student( char * pName = "no name", int xHours = 0, float xgpa = 0.0 )
{
cout << "constructing student " << pName << '\t' << xHours << '\t' << xgpa << endl;
name = new char[strlen(pName)+1];
if ( name != 0 )
{
strcpy( name, pName );
}
semesHours = xHours;
gpa = xgpa;
}
~Student()
{
cout << "destructing " << name << '\t' << semesHours << '\t' << gpa << endl;
delete [] name;
}
protected:
char * name;
int semesHours;
float gpa;
};
#include
#include
class Student
{
public:
Student(char* pName,int xHours,float xgpa)
{
cout<<"constructing student "<
name=new char[strlen(pName)+1];
if(name!=0)
{
strcpy(name,pName);
}
semesHours=xHours;
gpa=xgpa;
}
~Student()
{
if(name)
{
cout<<"destructing "<
delete name;
}
}
Student()
{
name = NULL;
semesHours = 0;
gpa = 0;
}
protected:
char * name;
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout<<"constructing teacher.\n";
}
~Teacher()
{
cout<<"destructing teacher.\n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout<<"construting tutorpair.\n";
noMeetings=0;
}
~TutorPair()
{
cout<<"destructing tutorpair.\n";
}
protected:
Student student;
Teacher teacher;
int noMeetings;
};
void main()
{
Student ss("Jenny",22,3.5);
cout<<"\n";
TutorPair fg;
cout<<"back in main.\n";
}
//记得给分哦
无法生成可执行文件