Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载'-';操作人员_C++ - Fatal编程技术网

C++ 重载'-';操作人员

C++ 重载'-';操作人员,c++,C++,目前,我正在编写一个程序,其中有一个部分用于确定两个日期之间的天数差,但是通过重载减号运算符 我现在正盯着我的屏幕画一张完全空白的图。我脑子里有一些转瞬即逝的想法,但它们正是如此,转瞬即逝 主要发生的是,有两个变量,例如贝多芬生日和贝多芬生日,将被减去,以确定他活了多久。如果我没记错的话,大约是22000天 不用多说,下面是我的代码: Date.cpp const std::string Date::MONTH_string[]= { “”,//基于一个的索引 “一月”, “二月”, “三月”,

目前,我正在编写一个程序,其中有一个部分用于确定两个日期之间的天数差,但是通过重载减号运算符

我现在正盯着我的屏幕画一张完全空白的图。我脑子里有一些转瞬即逝的想法,但它们正是如此,转瞬即逝

主要发生的是,有两个变量,例如
贝多芬生日
贝多芬生日
,将被减去,以确定他活了多久。如果我没记错的话,大约是22000天

不用多说,下面是我的代码:

Date.cpp
const std::string Date::MONTH_string[]=
{
“”,//基于一个的索引
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
};
const int Date::天/月[]=
{
0,//基于一个的索引
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};      
日期:日期(整数天,整数月,整数年):\u年(年),\u月(月),\u日(日)
{
isValid();
}
日期::日期()
{
time_t=time(0);//立即获取时间
struct tm*now=localtime(&t);
_年份=现在->tm_年份+1900;
_月份=现在->周一+一;
_白天=现在->白天;
}
整数日期::maxDay(整数月,整数年)
{
int ret=每月[月]天数;
如果(isLeapYear(year)=true&&month==2)
{
++ret;
}
返回ret;
}
作废日期::添加日期(bool forward)
{
如果(转发)
{
如果(_日<最大日(_月,_年))
{
++_一天;
}
其他的
{
_天=分钟/天;
++_月份;
如果(\u月>最大\u月)
{
_月=最小月;
++_年份;
}    
}
}
其他的
{

if(_day函数既可以作为成员编写,也可以作为自由函数编写。成员函数签名如下:

TimeDuration Date::operator-(Date const & rhs) const
TimeDuration operator-(Date const & lhs, Date const & rhs)
if lhs_date comes before rhs_date
    add days to (a copy of) lhs_date until lhs_date == rhs_date
    return the negative of number of days added
if rhs_date comes before lhs_date
    add days to (a copy of) rhs_date until rhs_date == lhs_date
    return the number of days added
else
    return 0
Date Date::operator-(TimeDuration rhs) const // member version
Date operator-(Date const & lhs, TimeDuration const & rhs) // non-member version
自由函数如下所示:

TimeDuration Date::operator-(Date const & rhs) const
TimeDuration operator-(Date const & lhs, Date const & rhs)
if lhs_date comes before rhs_date
    add days to (a copy of) lhs_date until lhs_date == rhs_date
    return the negative of number of days added
if rhs_date comes before lhs_date
    add days to (a copy of) rhs_date until rhs_date == lhs_date
    return the number of days added
else
    return 0
Date Date::operator-(TimeDuration rhs) const // member version
Date operator-(Date const & lhs, TimeDuration const & rhs) // non-member version
TimeDuration
这里有一个表示时间长度的完全独立的类型。如果您愿意,您可以将其设置为
int
,表示天数,但在我看来,最好有一个更具表现力的类型。无论您决定返回类型如何,它对您来说都没有任何意义他输入的是
Date
(当然不是
Date&

考虑到您已经编写了一个函数来向日期添加一天,一个可能的(尽管效率不高)实现是这样的:

TimeDuration Date::operator-(Date const & rhs) const
TimeDuration operator-(Date const & lhs, Date const & rhs)
if lhs_date comes before rhs_date
    add days to (a copy of) lhs_date until lhs_date == rhs_date
    return the negative of number of days added
if rhs_date comes before lhs_date
    add days to (a copy of) rhs_date until rhs_date == lhs_date
    return the number of days added
else
    return 0
Date Date::operator-(TimeDuration rhs) const // member version
Date operator-(Date const & lhs, TimeDuration const & rhs) // non-member version
您可能想要的另一个函数(或者这可能是您最初实际想要的,但您的措辞没有指明)是一个可以从
日期
中减去时间长度的函数。在这种情况下,返回值将是另一个
日期
对象(但不是
日期&
),可能的签名如下所示:

TimeDuration Date::operator-(Date const & rhs) const
TimeDuration operator-(Date const & lhs, Date const & rhs)
if lhs_date comes before rhs_date
    add days to (a copy of) lhs_date until lhs_date == rhs_date
    return the negative of number of days added
if rhs_date comes before lhs_date
    add days to (a copy of) rhs_date until rhs_date == lhs_date
    return the number of days added
else
    return 0
Date Date::operator-(TimeDuration rhs) const // member version
Date operator-(Date const & lhs, TimeDuration const & rhs) // non-member version
您应该这样做:

//This is where I get stumped (the parameters was just one of my failed experiments
TimeDuration& Date::operator-(Date const & d1)
{
    // ... processing ...
    // this - d1;
}
并称之为:

Date d1 = new Date(20, 01, 2013);
TimeDuration duration = d1 - (new const Date(20, 01, 1922));
// Calculate no. of days or years using duration
逻辑如下:

  • 将两个
    Date
    对象(第一个可以是隐式的)传递给重载函数并返回
    TimeDuration

  • 要调用此运算符,可以使用您拥有的数据创建一个
    Date
    对象,而不是单独传递每个值

  • 请检查确切的语法


    你想从日期中减去天、月或年吗?我这样问是因为在你的其他重载算术运算符中,你只有
    days
    作为一个参数。这两者有不同的目的。我现在正在从一本书中学习“赋值”对重载+的调用只是将两件事情相加,其中-被显式调用以确定两个被调用变量之间的天数差。因此,如果是这种情况,您希望将一个日期对象作为参数,并查找两个日期之间的天数差,而不是像现在这样接受3个参数。fir标准中的st“s”表示“短”:已经解决了问题:@AndrewB确切地说--
    lhs-rhs
    @AndrewB您可以用天数来规范化所有内容并执行差异。@AndrewB:您编写了一个函数来添加一天。反转逻辑应该相当简单,生成一个减去一天的函数。无论持续时间是多少天,都可以重复该操作。而不是mo最有效的解决方案,但我将把优化留给您。至于
    TimeDuration
    ,它还不存在。它由您定义。正如我所说,您可以使用
    int
    (表示天数)取而代之的是,我更喜欢更具表现力的类型。如果你只使用
    int
    ,你必须记录
    int
    代表什么。对于名为
    TimeDuration
    的类型,你不需要。@AndrewB:注意
    TimeDuration
    类可能只是
    int
    代表天数,但这是用户不必关心的实现细节。成员函数版本应声明为
    const
    dated1=new Date(…)这是什么?不是C++,当然。BjjayLundLee,你是对的。这就是为什么我在回答的最后提到了要检查确切的语法。我提供的解决方案不是一个精确的复制粘贴解决方案。这只是为了指导ASKER执行正确的逻辑。如果你找到了一个,请告诉我。逻辑出了问题。