error C2629: unexpected \'class Date (\'

错误:

F:\C++练习\f0903.cpp(14) : error C2629: unexpected 'class Date ('
F:\C++练习\f0903.cpp(14) : error C2238: unexpected token(s) preceding ';'

源程序:

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

class Date
{
int year, month, day;

public:
  
   Date(int y=2000, int m=1, int d=1);

  Date(const string& s);
   bool isLeapYear() const;
   friend ostream& operator<<(ostream & o, const Date& d);
};

Date::Date(const 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() );
}


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

bool Date::isLeapYear()const
{
return ( year%4==0 && year%100!=0)|| year%400==0;
}

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(' ');
}

int main()
{
Date c("2005-12-28");
Date d(2003, 12, 6);
Date e(2002);
Date f(2002, 12);
Date g;
cout<<c<<d<<e<<f<<g;
return 0;
}

原因:1.一个定义错误,一个少个分号

       2.vc对<<支持的问题。使用gcc。    还可以屏蔽use namespace std;    改为std::。

      3.#include <string> 可以使用string类,当然可能还需要using namespace std,假如不像std::string这       么用的话

       4.#include<string>与#include<string.h>有区别

解决:将绿色代码以下代码

Date(const std::string& s);

Date:: Date(const std::string& s);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值