//我的编译器是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,我的是盗版的)通过