abnormal program termination

26 篇文章 0 订阅

//我的编译器是VC6.0

#include<iostream.h>
#include<iomanip.h>
#include<string>
//using namespace std;

class Date{
int year, month, day;
void init();
public:
Date(const std::string& s);
Date(int y=2000, int m=1, int d=1);
void display();
friend ostream& operator<<(ostream& o, const Date& d);
};

void Date::init(){
if(year>5000 || year<1 || month<1 || month>12 || day<1 || day>31)
exit(1);
}

Date::Date(const std::string& s){
year=atoi(s.substr(0, 4).c_str() );
month=atoi(s.substr(5,2).c_str() );
day=atoi(s.substr(8, 2).c_str() );
init();
}

Date::Date(int y, int m, int d){
year=y, month=m, day=d;
init();
}

void Date::display()
{
cout<<year<<"-"<<month<<"-"<<day;
}

ostream& operator<<(ostream& o, const Date& d) {
o<<setfill('0')<<setw(4)<<d.year<<'-'<<setw(2)<<d.month<<'-';
return o<<setw(2)<<d.day<<'\n'<<setfill(' ');
}

class Wdate:public Date
{
std::string week;
public:
Wdate(int y, int m, int d, std::string w);
};


Wdate::Wdate(int y, int m, int d, std::string w):Date(y, m, d), week(w)
{
Date::display();
cout<<week<<endl;//出错处
}

int main(){
Date c("2005-08-21");
cout<<c<<endl;

Wdate e(2008, 9, 11, "4");

return 0;
}

解决:1.绿色部分改为:cout<<week.c_str()<<endl;

      注: const char *c_str()const;//返回一个以null终止的c字符串          

        2.别人用G++,VS2005(甚至VC6.0,我的是盗版的)通过

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值