错在哪里了?怎么改正?

cizi_nwu 2005-01-07 02:53:42
以下代码在dev C++中编译的时候总是提示Month Date::month() const;这个成员函数定义出错,提示是:syntax error before '::' ,这是怎么回事?谢谢!


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

class Date
{
public:
enum Month{jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
class Bad_date{}; //exceptions
Date(int dd = 0, Month mm = Month(0), int yy = 0);

static void set_default(int, Month, int);

//function for examining the Date
int day() const;
Month month() const;
int year() const;

private:
int d, y;
int m;
static Date default_date;
bool leapyear(int yy);
};


Date::Date(int dd, Month mm, int yy)
{
if (yy == 0)
{
yy = default_date.year();
}
if (mm == 0)
{
mm = default_date.month();
}
if (dd == 0)
{
dd = default_date.day();
}

//check that the Date is valid
int max;

switch (mm)
{
case feb:
max = 28 + leapyear(yy);
break;
case apr: case jun: case sep: case nov:
max = 30;
break;
case jan: case mar: case may: case jul: case aug: case oct: case dec:
max = 31;
break;
default:
throw Bad_date();
}

if ((dd < 1 )|| (max < dd))
{
throw Bad_date();
}

y = yy;
m = mm;
d = dd;
}


Date Date::default_date(16, dec, 1770);

void Date::set_default(int d, Month m, int y)
{
Date::default_date = Date(d, m, y);
}

inline int Date::day() const
{
return d;
}

inline Month Date::month() const //总是提示该行:syntax error before '::'
{
return m;
}

inline int Date::year() const
{
return y;
}

bool Date::leapyear(int yy)
{
return false;
}

int main()
{
Date today;

system("PAUSE");
return 0;
}


...全文
137 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen_zhaozhencn 2005-01-07
  • 打赏
  • 举报
回复
class Date
{
...
int m; 改成: Month m;

};


//month改成:
inline Date::Month Date::month() const
{
return m;
}

cizi_nwu 2005-01-07
  • 打赏
  • 举报
回复
rogeryi(Roger)
谢谢,按你说的编译通过了,我再试试看又没有其它问题!
非常感谢,问题搞定给你分!哈哈!谢谢!
playmud 2005-01-07
  • 打赏
  • 举报
回复
inline Date::Month Date::month() const //总是提示该行:syntax error before '::'
{
return m;
}
不过你需要把Int类型转换成Month
易旭昕 2005-01-07
  • 打赏
  • 举报
回复
编译试了一下,前面的问题解决了,
不过 return m; 出错。
虽然enum实际上是一个int,不过直接用int取代enum类型不能通过类型检测的。
易旭昕 2005-01-07
  • 打赏
  • 举报
回复
inline Date::Month Date::month() const 吧,
觉得应该是Month数据类型没有标志清楚,
不过没有实际编译试过。
yevv 2005-01-07
  • 打赏
  • 举报
回复
inline Month Date::month() const //总是提示该行:syntax error before '::'

Month 是啥东西 应该是int吧

64,649

社区成员

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

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