Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/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++;重载运算符-减去两个相同类型的对象 P>试着跟上这本书的自学进度C++。_C++_Operator Overloading_Overloading - Fatal编程技术网

C++;重载运算符-减去两个相同类型的对象 P>试着跟上这本书的自学进度C++。

C++;重载运算符-减去两个相同类型的对象 P>试着跟上这本书的自学进度C++。,c++,operator-overloading,overloading,C++,Operator Overloading,Overloading,到目前为止,我已经创建了一个程序,它有一个日期对象,有四个参数。用户填写的三个参数是“月”、“日”和“年”。第四个参数是“Old”,即存储它有多少天的历史。为此,我编写了一个函数来计算它有多少天了,并将其设置为第四个参数 这部分是有效的。它计算它有多少天了 这本书想让我减去两个Date类型的对象,找出它们的年龄差异。为此,它特别要求我创建一个重载运算符(-),并减去这两个。这就是我被卡住的地方。我认为,我遇到问题的部分是用来访问它的呼叫。我继续得到“运算符-”的“不明确重载”错误(操作数类型为“

到目前为止,我已经创建了一个程序,它有一个日期对象,有四个参数。用户填写的三个参数是“月”、“日”和“年”。第四个参数是“Old”,即存储它有多少天的历史。为此,我编写了一个函数来计算它有多少天了,并将其设置为第四个参数

这部分是有效的。它计算它有多少天了

这本书想让我减去两个Date类型的对象,找出它们的年龄差异。为此,它特别要求我创建一个重载运算符(-),并减去这两个。这就是我被卡住的地方。我认为,我遇到问题的部分是用来访问它的呼叫。我继续得到“运算符-”的“不明确重载”错误(操作数类型为“日期”和“日期”)

它将两个对象都识别为日期。现在我只需要一点帮助来完成最后一段

我曾尝试在标题中使用几种不同的东西,但可能我没有正确地实现它们

-friend Date operator - (Date &ob1, Date &ob2);
-Date &operator-();
-Date &operator-(Date);
-Date operator-(Date);
如果你能帮上忙,那就太好了。我是新来的,我在这个问题上花了12个多小时

Main.cpp

#include <iostream>
#include <time.h>
#include <ctime>
#include "Date.cpp" // Date class definition
using namespace std;

int main() {
    unsigned int birthMonth = 0;
    unsigned int birthDay = 0;
    unsigned int birthYear = 0;

    unsigned int dateToMonth = 0;
    unsigned int dateToDay = 0;
    unsigned int dateToYear = 0;

    cout << "Enter birth month (1-12): ";
    cin >> birthMonth;
    cout << "Enter birth day (1-31): ";
    cin >> birthDay;
    cout << "Enter birth year (1900 - 2000): ";
    cin >> birthYear;

    Date birthDate (birthMonth, birthDay, birthYear);
    Date tempDate (1, 1, 1, 1);

    cout << "To which date would you like to calculate to?\nEnter Day month (1-12): ";
    cin >> dateToMonth;
    cout << "Enter Day to calculate it to (1-31): ";
    cin >> dateToDay;
    cout << "Enter Year to calculate it to: ";
    cin >> dateToYear;

    Date dateTo (dateToMonth, dateToDay, dateToYear);

    pastDays(birthDate);
    pastDays(dateTo);

    cout << "\nHow many days ago is the birth date? " << birthDate.old << endl;
    cout << "How many days ago is the secondary date? " << dateTo.old << endl;

    // Here is where I get an error "ambiguous overload for "operator-' (operand types are 'Date' and 'Date')"
    cout << tempDate = birthDate - dateTo << endl;
    cout << tempDate.old;
}
#包括
#包括
#包括
#包括“Date.cpp”//Date类定义
使用名称空间std;
int main(){
无符号整数生日月=0;
无符号整数=0;
无符号整数生日=0;
无符号整数dateToMonth=0;
unsigned int dateToDay=0;
无符号整数dateToYear=0;
cout>出生月份;
生日;
生日;
日期出生日期(出生月、生日、出生年);
日期tempDate(1,1,1,1);
cout>dateToMonth;
今天不能约会;
日期>年份;
Date dateTo(DateTomon,dateToDay,dateToYear);
过去的日子(生日);
过去日期(日期至);

无法使用复合赋值运算符(例如
-=
)定义二进制算术运算符(例如-)。您可能需要查看此参考:

Date& operator-=(const X& rhs) {                           
  /* subtraction of rhs to *this takes place here */
  this->year - rhs.year; // This is the easy one

  /* Here you will have to play around with this->month and this->day */

  return *this; // return the result by reference
}


friend Date operator-(Date lhs, const Date& rhs) {
  lhs -= rhs; // use compound assignment
  return lhs; // return the result by value
}
main.cpp

#include <iostream>
#include <time.h>
#include <ctime>
#include "Date.cpp" // Date class definition
using namespace std;

int main() {
    unsigned int birthMonth = 0;
    unsigned int birthDay = 0;
    unsigned int birthYear = 0;

    unsigned int dateToMonth = 0;
    unsigned int dateToDay = 0;
    unsigned int dateToYear = 0;

    cout << "Enter birth month (1-12): ";
    cin >> birthMonth;
    cout << "Enter birth day (1-31): ";
    cin >> birthDay;
    cout << "Enter birth year (1900 - 2000): ";
    cin >> birthYear;

    Date birthDate (birthMonth, birthDay, birthYear);
    Date tempDate (1, 1, 1, 1);

    cout << "To which date would you like to calculate to?\nEnter Day month (1-12): ";
    cin >> dateToMonth;
    cout << "Enter Day to calculate it to (1-31): ";
    cin >> dateToDay;
    cout << "Enter Year to calculate it to: ";
    cin >> dateToYear;

    Date dateTo (dateToMonth, dateToDay, dateToYear);

    pastDays(birthDate);
    pastDays(dateTo);

    cout << "\nHow many days ago is the birth date? " << birthDate.old << endl;
    cout << "How many days ago is the secondary date? " << dateTo.old << endl;

    tempDate = birthDate - dateTo;
    cout << tempDate.old;
}
#包括
#包括
#包括
#包括“Date.cpp”//Date类定义
使用名称空间std;
int main(){
无符号整数生日月=0;
无符号整数=0;
无符号整数生日=0;
无符号整数dateToMonth=0;
unsigned int dateToDay=0;
无符号整数dateToYear=0;
cout>出生月份;
生日;
生日;
日期出生日期(出生月、生日、出生年);
日期tempDate(1,1,1,1);
cout>dateToMonth;
今天不能约会;
日期>年份;
Date dateTo(DateTomon,dateToDay,dateToYear);
过去的日子(生日);
过去日期(日期至);

您可能想检查一下,例如?@特别是@Someprogrammerdude谢谢。主要部分建议使用friend,因此我修改了标题以包含它,并将函数更改为…friend Date运算符-(Date&date1,Date&date2)。不幸的是,我在课外使用了“friend”错误这与昨天的问题有何不同?[OT]:
#包括“Date.cpp”
,您不应该这样做,您应该创建一个包含多个文件的项目。@BoPersson这是一个主要的代码大修,现在它将Daysald函数存储在对象中,不像以前它不能这样做。这段代码可以计算指定日期的前几天。我想现在它不同了足够尝试并重新提交,并指定它必须是重载的-使用Date函数作为返回,而不是int。对于顶部位,编译器抱怨它需要两个参数。对于这一点,因为只需要更新旧参数,所以顶部部分是…Date&operator-=(Date lhs,const Date&rhs){lhs=lhs.old-rhs.old;return lhs;}当我这样做时,它没有给我任何错误。它给了我一个关于friend的错误,在类外使用它。这是否意味着它必须保留在头文件中?我从它删除了friend,以查看它是否可以工作,但得到了“operator-”的这个错误重载(操作数类型为“日期”和“日期”)|
Date& operator-=(const X& rhs) {                           
  /* subtraction of rhs to *this takes place here */
  this->year - rhs.year; // This is the easy one

  /* Here you will have to play around with this->month and this->day */

  return *this; // return the result by reference
}


friend Date operator-(Date lhs, const Date& rhs) {
  lhs -= rhs; // use compound assignment
  return lhs; // return the result by value
}
#include <iostream>
#include <time.h>
#include <ctime>
#include "Date.cpp" // Date class definition
using namespace std;

int main() {
    unsigned int birthMonth = 0;
    unsigned int birthDay = 0;
    unsigned int birthYear = 0;

    unsigned int dateToMonth = 0;
    unsigned int dateToDay = 0;
    unsigned int dateToYear = 0;

    cout << "Enter birth month (1-12): ";
    cin >> birthMonth;
    cout << "Enter birth day (1-31): ";
    cin >> birthDay;
    cout << "Enter birth year (1900 - 2000): ";
    cin >> birthYear;

    Date birthDate (birthMonth, birthDay, birthYear);
    Date tempDate (1, 1, 1, 1);

    cout << "To which date would you like to calculate to?\nEnter Day month (1-12): ";
    cin >> dateToMonth;
    cout << "Enter Day to calculate it to (1-31): ";
    cin >> dateToDay;
    cout << "Enter Year to calculate it to: ";
    cin >> dateToYear;

    Date dateTo (dateToMonth, dateToDay, dateToYear);

    pastDays(birthDate);
    pastDays(dateTo);

    cout << "\nHow many days ago is the birth date? " << birthDate.old << endl;
    cout << "How many days ago is the secondary date? " << dateTo.old << endl;

    tempDate = birthDate - dateTo;
    cout << tempDate.old;
}
#ifndef DATE_H
#define DATE_H
#include <array>
#include <iostream>

class Date
  {
    friend std::ostream &operator<<( std::ostream &, const Date & );
//  friend Date operator-(Date, Date);
public:
    Date( int m = 1, int d = 1, int y = 1900, int o = 0 ); // default constructor
    void setDate( int, int, int, int ); // set month, day, year
//  friend Date operator-(Date, Date);
    Date operator-(); // Modified Line for Assignment
    Date operator-(Date); // Modified Line for Assignment
//    Date operator-(Date);
    void pastDays (Date);
    static bool leapYear( int ); // is date in a leap year?
    unsigned int month;
    unsigned int day;
    unsigned int year;
    int old;
    static const std::array< unsigned int, 13 > days; // days per month
}; // end class Date

#endif
#include <iostream>
#include <string>
#include "Date.h"
#include <time.h>
#include <ctime>
#include <conio.h>
#include "Date.h" // Date class definition
using namespace std;

   // initialize static member; one classwide copy
const array< unsigned int, 13 > Date::days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Date constructor
Date::Date( int month, int day, int year, int old )
  {
    setDate( month, day, year, old );
  } // end Date constructor

// set month, day and year
void Date::setDate( int mm, int dd, int yy, int old )
  {
    if ( mm >= 1 && mm <= 12 )
        month = mm;
    else
        throw invalid_argument( "Month must be 1-12" );

    if ( yy >= 1900 && yy <= 2100 )
        year = yy;

    if ( ( month == 2 && leapYear( year ) && dd >= 1 && dd <= 29 ) ||
        ( dd >= 1 && dd <= days[ month ] ) )
        day = dd;
    else
        throw invalid_argument(
            "Day is out of range for current month and year" );


} // end function setDate

void pastDays (Date &entryDay) {
    //Creating Today date object
    time_t t = time(0);
    struct tm * now = localtime(&t);
    int currentYear = now -> tm_year + 1900;
    int currentMonth = now -> tm_mon + 1;
    int currentDay = now -> tm_mday;

    int birthMonth = entryDay.month;
    int birthDay = entryDay.day;
    int birthYear = entryDay.year;

    //The variable that will be assigned to the old parameter, which can then be subtracted from another time.
    int daysAgo = 0;
    entryDay.old = 0;

    cout << endl;
    cout << "First" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    //Lowering days to 1, to make transition between years easier.
//    while (birthDay > 1){
//        birthDay--;
//        daysAgo--;
//    }

    daysAgo = daysAgo - birthDay;
    daysAgo++;
    birthDay = 1;

    cout << endl;
    cout << "Second" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    //Lowering months to 1, to make transition between years easier.
    while (birthMonth > 1){
        if (birthMonth == 1 || birthMonth == 3 || birthMonth == 5 || birthMonth == 7 || birthMonth == 8 || birthMonth == 10 || birthMonth == 12){
            birthMonth--;
            daysAgo -= 31;
        }
        if (birthMonth == 4 || birthMonth == 6 || birthMonth == 9 || birthMonth == 11){
            birthMonth--;
            daysAgo -= 30;
        }
        if (birthMonth == 2)
            if ( currentYear % 400 == 0 ||
            ( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
    birthMonth--;
    daysAgo -= 29;
            }
        else {
            birthMonth--;
            daysAgo -= 28;
        }
    }

    cout << endl;
    cout << "Third" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    //Incrementing year to current year
    while (birthYear < currentYear){
        if ( currentYear % 400 == 0 ||
        ( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
            daysAgo = daysAgo + 366;
            birthYear++;
        }
        else {
            daysAgo = daysAgo + 365;
            birthYear++;
        }
    }

    cout << endl;
    cout << "Fourth" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    // Incrementing to current month
    while (birthMonth < currentMonth) {
        if (birthMonth == 1 || birthMonth == 3 || birthMonth == 5 || birthMonth == 7 || birthMonth == 8 || birthMonth == 10 || birthMonth == 12){
            birthMonth++;
            daysAgo += 31;
        }
        if (birthMonth == 4 || birthMonth == 6 || birthMonth == 9 || birthMonth == 11){
            birthMonth++;
            daysAgo += 30;
        }
        if (birthMonth == 2)
            if ( currentYear % 400 == 0 ||
            ( currentYear % 100 != 0 && currentYear % 4 == 0 ) ) {
    birthMonth++;
    daysAgo += 29;
            }
        else {
            birthMonth++;
            daysAgo += 28;
        }
    }

    cout << endl;
    cout << "Fifth" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    //Incrementing to current day, and adding the days to the daysAgo
    while (birthDay < currentDay){
        birthDay++;
        daysAgo++;
    }


    cout << endl;
    cout << "Sixth" << daysAgo << endl;
    cout << "BirthMonth: " << birthMonth << endl;
    cout << "BirthDay: " << birthDay << endl;
    cout << "BirthYear: " << birthYear << endl;

    //Assigning DaysAgo to input parameter.old
    entryDay.old = daysAgo;
}

bool Date::leapYear( int testYear )
{
    if ( testYear % 400 == 0 ||
      ( testYear % 100 != 0 && testYear % 4 == 0 ) )
        return true; // a leap year
    else
        return false; // not a leap year
} // end function leapYear

// overloaded output operator
ostream &operator<<( ostream &output, const Date &d )
{
  static string monthName[ 13 ] = { "", "January", "February",
        "March", "April", "May", "June", "July", "August",
        "September", "October", "November", "December" };
  output << monthName[ d.month ] << ' ' << d.day << ", " << d.year;
  return output; // enables cascading
} // end function operator<<

Date Date::operator-(Date rhs) {
  //lhs -= rhs; // use compound assignment
  //return lhs; // return the result by value
  Date n = *this;
  n.old -= rhs.old;
  return n;
}