求解为什么会闪退报错

mj122304532 2015-05-29 11:39:03
#include <iostream>
#include<string>
const int SIZE = 20;
using namespace std;

class Cd
{
private:
char* performers;
char* label;
int selections; //曲目数
double playtime; //播放时间
public:
Cd(const char * s1, const char * s2, int n, double x);
~Cd(){};
virtual void Report() const; // 显示CD所有数据
};

Cd::Cd(const char * s1 = new char[SIZE], const char * s2 = new char[SIZE], int n=0, double x=0.0)
{
strcpy_s(performers,222,s1);
strcpy_s(label,222,s2);
selections = n;
playtime = x;
}

void Cd::Report() const
{
cout << performers << ", " << label << ", " << selections << ", " << playtime << endl;
}

/**************************************************************/
class Classic :public Cd
{
private:
char* performers;
char* label;
char* symple;
int selections; //曲目数
double playtime; //播放时间
public:
Classic(const char * s1, const char * s2, const char * s3, int n, double x);
virtual void Report() const;
char primarywork();
};

Classic::Classic(const char * s1 = new char[SIZE], const char * s2 = new char[SIZE], const char * s3 = new char[SIZE], int n=0, double x=0.0)
{
strcpy_s(performers, 222, s1);
strcpy_s(label, 222, s2);
strcpy_s(symple, 222, s3);
selections = n;
playtime = x;
}

void Classic::Report() const
{
cout << performers << ", " << label << ", " << symple << ", " << selections << ", " << playtime<< endl;
}

char Classic::primarywork()
{
char message;
cout << "please input the message of the CD('q' is QUIT):" << endl;
cin >> message;
while (message =='q')
{
cout << message;
}
return message;
}


/**************************************************************/

void Bravo(const Cd & disk);

int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1;
cout << "Using object directly:\n";
c1.Report();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);


system("pause");
return 0;
}

void Bravo(const Cd & disk)
{
disk.Report();
}


求解决办法
...全文
546 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2015-05-29
  • 打赏
  • 举报
回复
Classic(const char * s1 = new char[SIZE], const char * s2 = new char[SIZE], const char * s3 = new char[SIZE], int n=0, double x=0.0) 这个又是什么用法呢? 不是performers = new char[SIZE];么
pengzhixi 2015-05-29
  • 打赏
  • 举报
回复
strcpy_s(performers,222,s1);//你确定你能拷贝222个字符进去?
pengzhixi 2015-05-29
  • 打赏
  • 举报
回复
#include <iostream>
#include<string>
#include<cstring>
const int SIZE = 20;
using namespace std;

class Cd 
{
private:
char* performers;
char* label;
int selections;                  //曲目数
double playtime;                //播放时间
public:
Cd(const char * s1, const char * s2, int n, double x);
~Cd(){};
virtual void Report() const;         // 显示CD所有数据
};

Cd::Cd(const char * s1, const char * s2 , int n=0, double x=0.0)
{
performers	= new char[SIZE];
label	= new char[SIZE];
strcpy(performers,s1);
strcpy(label,s2);
selections = n;
playtime = x;
}

void Cd::Report() const
{
cout << performers << ", " << label << ", " << selections << ", " << playtime << endl;
}

/**************************************************************/
class Classic :public Cd
{
private:
char* symple;
public:
Classic(const char * s1, const char * s2, const char * s3, int n, double x);
virtual void Report() const;
char primarywork();
};

Classic::Classic(const char * s1 , const char * s2 , const char * s3, int n=0, double x=0.0):Cd(s1,s2,n,x)
{
symple=new char[SIZE];
strcpy(symple, s3);

}

void Classic::Report() const
{   
  this->Cd::Report();
  cout<<symple<<endl;
}

char Classic::primarywork()
{
char message;
cout << "please input the message of the CD('q' is QUIT):" << endl;
cin >> message;
while (message =='q')
{
cout << message;
}
return message;
}


/**************************************************************/

void Bravo(const Cd & disk);

int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
Cd *pcd = &c1;
cout << "Using object directly:\n";
c1.Report();
c2.Report();
cout << "Using type cd * pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);


system("pause");
return 0;
}

void Bravo(const Cd & disk)
{
disk.Report();
}
赵4老师 2015-05-29
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
mj122304532 2015-05-29
  • 打赏
  • 举报
回复
引用 2 楼 pengzhixi 的回复:
Classic(const char * s1 = new char[SIZE], const char * s2 = new char[SIZE], const char * s3 = new char[SIZE], int n=0, double x=0.0) 这个又是什么用法呢? 不是performers = new char[SIZE];么
#include <iostream> #include<string> const int SIZE = 10; using namespace std; class Cd { private: char* performers= new char[SIZE]; char* label = new char[SIZE]; int selections ; //曲目数 double playtime; //播放时间 public: Cd(const char * s1, const char * s2, int n, double x); Cd(){}; ~Cd(){}; virtual void Report() const; // 显示CD所有数据 }; Cd::Cd(const char * s1, const char * s2 , int n, double x) { strcpy_s(performers,10,s1); strcpy_s(label,10,s2); selections = n; playtime = x; } void Cd::Report() const { cout << performers << ", " << label << ", " << selections << ", " << playtime << endl; } /**************************************************************/ class Classic :public Cd { private: char* performers = new char[SIZE]; char* label = new char[SIZE]; char* symple = new char[SIZE]; int selections; //曲目数 double playtime; //播放时间 public: Classic(const char * s1, const char * s2, const char * s3, int n, double x); virtual void Report() const; char primarywork(); }; Classic::Classic(const char * s1, const char * s2, const char * s3, int n , double x ) : Cd(s1, s2, n, x) { strcpy_s(performers, 10, s1); strcpy_s(label, 10, s2); strcpy_s(symple, 10, s3); selections = n; playtime = x; } void Classic::Report() const { cout << performers << ", " << label << ", " << symple << ", " << selections << ", " << playtime<< endl; } char Classic::primarywork() { char message; cout << "please input the message of the CD('q' is QUIT):" << endl; cin >> message; while (message =='q') { cout << message; } return message; } /**************************************************************/ void Bravo(const Cd & disk); int main() { Cd c1("Beatles", "Capitol", 14, 35.5); Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17); Cd *pcd = &c1; cout << "Using object directly:\n"; c1.Report(); c2.Report(); cout << "Using type cd * pointer to objects:\n"; pcd->Report(); pcd = &c2; pcd->Report(); cout << "Calling a function with a Cd reference argument:\n"; Bravo(c1); Bravo(c2); system("pause"); return 0; } void Bravo(const Cd & disk) { disk.Report(); } 是这样吗 但是还是我发运行

64,675

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧