在visual c++ 6.0中运行有错误,dev中缺没问题

走好每一步 2008-10-07 06:42:16
// 出版物
class CPublication{
private:
char * p_title;
float price;
char date[20];
bool on_loan;
public:
CPublication(char *p_t, float p, char *p_d);
void set(char *p_t, float p, char *p_d);
bool borrowIt();
bool returnIt();
void print()const;
~CPublication() { delete [] p_title; }
};
#include <iostream>
#include <cstring>
using namespace std;

CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_title)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
void CPublication::set(char *p_t, float p, char *p_d){
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
bool CPublication::borrowIt(){
if (on_loan == true){
on_loan = false;
return true;
}
else{
cout << "The book has been gone through." << endl;
return false;
}
}
bool CPublication::returnIt(){
on_loan = true;
return true;
}
void CPublication::print()const{
cout << "书名:" << p_title << endl;
cout << "价格:" << price << endl;
cout << "出版日期:" << date << endl;
if (on_loan == true)
cout << "可供借出" << endl;
else
cout << "已借出" << endl;
}

int main(){
CPublication shu("语文课本", 45.0, "2008.10.6");
shu.borrowIt();
shu.returnIt();
shu.print();
system("pause");
return 0;
}


...全文
85 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
走好每一步 2008-10-10
  • 打赏
  • 举报
回复
这几天校园网老是搞掉线,终于来了,结贴了
backway 2008-10-07
  • 打赏
  • 举报
回复
p_title = new char [strlen(p_t)+1];
disciple 2008-10-07
  • 打赏
  • 举报
回复
同意1楼,楼主大意了
迷途的书童 2008-10-07
  • 打赏
  • 举报
回复
楼上!太强啦!顶
chlaws 2008-10-07
  • 打赏
  • 举报
回复
p_title = new char [/*strlen(p_title)+*/100]; strlen(p_title) 出错
太乙 2008-10-07
  • 打赏
  • 举报
回复


#include <iostream>
#include <cstring>
using namespace std;


// ³ö°æÎï
class CPublication{
private:
char * p_title;
float price;
char date[20];
bool on_loan;
public:
CPublication(char *p_t, float p, char *p_d);
void set(char *p_t, float p, char *p_d);
bool borrowIt();
bool returnIt();
void print()const;
~CPublication() { delete [] p_title; }
};

CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
void CPublication::set(char *p_t, float p, char *p_d){
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
bool CPublication::borrowIt(){
if (on_loan == true){
on_loan = false;
return true;
}
else{
cout << "The book has been gone through." << endl;
return false;
}
}
bool CPublication::returnIt(){
on_loan = true;
return true;
}
void CPublication::print()const{
cout << "ÊéÃû£º" << p_title << endl;
cout << "¼Û¸ñ£º" << price << endl;
cout << "³ö°æÈÕÆÚ£º" << date << endl;
if (on_loan == true)
cout << "¿É¹©½è³ö" << endl;
else
cout << "Òѽè³ö" << endl;
}

int main(){

CPublication shu("ÓïÎĿα¾", 45.0, "2008.10.6");
shu.borrowIt();
shu.returnIt();
shu.print();
system("pause");
return 0;
}


出错在这:




CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1]; //这里strlen的参数应该是p_t
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}


xkyx_cn 2008-10-07
  • 打赏
  • 举报
回复

class CPublication{
private:
char * p_title;
float price;
char date[20];
bool on_loan;
public:
CPublication(char *p_t, float p, char *p_d);
void set(char *p_t, float p, char *p_d);
bool borrowIt();
bool returnIt();
void print()const;
~CPublication() { delete [] p_title; }
};
#include <iostream>
#include <cstring>
using namespace std;

CPublication::CPublication(char *p_t=NULL, float p=0.0, char *p_d=NULL){
p_title = new char [strlen(p_t)+1]; // p_t写成p_title了,改过来就好了
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
void CPublication::set(char *p_t, float p, char *p_d){
delete [] p_title;
p_title = new char [strlen(p_t)+1];
strcpy (p_title, p_t);
price = p;
strcpy (date, p_d);
on_loan = true;
}
bool CPublication::borrowIt(){
if (on_loan == true){
on_loan = false;
return true;
}
else{
cout << "The book has been gone through." << endl;
return false;
}
}
bool CPublication::returnIt(){
on_loan = true;
return true;
}
void CPublication::print()const{
cout << "书名:" << p_title << endl;
cout << "价格:" << price << endl;
cout << "出版日期:" << date << endl;
if (on_loan == true)
cout << "可供借出" << endl;
else
cout << "已借出" << endl;
}

int main(){
CPublication shu("语文课本", 45.0, "2008.10.6");
shu.borrowIt();
shu.returnIt();
shu.print();
system("pause");
return 0;
}

64,664

社区成员

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

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