关于VC++6运行程序后出现“出现一个问题,导致程序停止正常工作。请关闭该程序”,望解决,谢谢!

2025-01-02 05:52:25
推荐回答(3个)
回答1:

问题在 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;
};

回答2:

#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 "< name[0]='\0';
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";

}
//记得给分哦

回答3:

无法生成可执行文件