在同一文件中使用 bool 类型转换运算符和 Comparison 运算符重载方法

Using bool type conversion operator and Comparison operator overload methods in the same file

提问人:Khaled Bais 提问时间:7/14/2023 更新时间:7/14/2023 访问量:55

问:

我正在尝试执行代码,但每次运行它时,它都使用了错误的运算符:

class Date {
   private:
      int m_year;
      int m_mon;
      int m_day;
      int m_ErrorCode;
      int m_CUR_YEAR;
      int daysSince0001_1_1()const; // returns number of days passed since the date 0001/1/1
      bool validate();             /* validates the date setting the error code and then returning the result 
                                    true, if valid, and false if invalid.*/
      void errCode(int);           // sets the error code
      int systemYear()const;       // returns the current system year
      bool bad()const;             // return true if
      int mdays()const;            // returns the number of days in current month
      void setToToday();           // sets the date to the current date (system date)
   public:
      Date();                      // creates a date with current date
      Date(int year, int mon, int day); /* create a date with assigned values
                                         then validates the date and sets the 
                                         error code accordingly */
      int errCode()const;         // returns the error code or zero if date is valid
      const char* dateStatus()const;  // returns a string corresponding the current status of the date
      int currentYear()const;         // returns the m_CUR_YEAR value;

      bool operator==(Date& date) const;
      bool operator!=(Date& date) const;
      bool operator>=(Date& date) const;
      bool operator<=(Date& date) const;
      bool operator<(Date& date) const;
      bool operator>(Date& date) const;

      int operator-(Date& date) const;

      operator bool() const;

      std::istream& read(std::istream& is = std::cin);
      std::ostream& write(std::ostream& os = std::cout)const;

      
   };

这是我的课堂模块

 bool Date::bad()const {
        return m_ErrorCode != 0;
    }

    bool Date::operator==(Date& date) const {
        return daysSince0001_1_1() == date.daysSince0001_1_1();
    }
    bool Date::operator!=(Date& date) const {
        return daysSince0001_1_1() != date.daysSince0001_1_1();
    }
    bool Date::operator>=(Date& date) const {
        return daysSince0001_1_1() >= date.daysSince0001_1_1();
    }
    bool Date::operator<=(Date& date) const {
        return daysSince0001_1_1() <= date.daysSince0001_1_1();
    }
    bool Date::operator<(Date& date) const {
        return daysSince0001_1_1() < date.daysSince0001_1_1();
    }
    bool Date::operator>(Date& date) const {
        return daysSince0001_1_1() > date.daysSince0001_1_1();
    }

    int Date::operator-(Date& date) const {
        return daysSince0001_1_1() - date.daysSince0001_1_1();
    }

    Date::operator bool() const {
        return !bad();
    }

这是我实现的一部分。 但是,如果我这样做:

if (B > A) {
        cout << B << " > " << A << endl;
    }
    else {
        cout << B << " <= " << A << endl;
    }
    if (B < A) {
        cout << B << " < " << A << endl;
    }
    else {
        cout << B << " >= " << A << endl;
    }
    if (B <= A) {
        cout << B << " <= " << A << endl;
    }
    else {
        cout << B << " > " << A << endl;
    }
    if (B >= A) {
        cout << B << " >= " << A << endl;
    }
    else {
        cout << B << " < " << A << endl;
    }
    if (B == A) {
        cout << B << " == " << A << endl;
    }
    else {
        cout << B << " != " << A << endl;
    }
    if (B != A) {
        cout << B << " != " << A << endl;
    }
    else {
        cout << B << " == " << A << endl;
    }
    cout << "Days between the two dates: " << B - A << endl;

它使用运算符 bool() const 而不是正确的函数

我已经尝试使用 chatgpt 并阅读有关重载方法的信息,但它没有改变

C++ 算符重载 布尔运算

评论

1赞 AndyG 7/14/2023
如果没有一个完整的最小可重现示例,很难说出来,但请考虑将比较函数的参数标记为 。例如,constbool operator==(const Date& date) const;
0赞 Eljay 7/14/2023
该参数告诉我“这是调用方参数的别名,此例程将更改调用方参数的值”。你打算吗?Date& dateDate const& date
0赞 Aconcagua 7/14/2023
旁注:如果你有可用的C++20,那么你可以简单地实现宇宙飞船运算符,而不是实现所有这些比较运算符:std::strong_ordering operator<=>(Date const& other) { return daysSince0001_1_1() <=> date.daysSince0001_1_1(); }
0赞 463035818_is_not_an_ai 7/14/2023
“它使用运算符 bool() const 而不是正确的函数”“它”是什么?什么是“正确的功能”?你怎么知道叫?operator bool
1赞 Aconcagua 7/14/2023
@Eljay '[...] 将 [潜在] 改变 [...]' 完全准确地说,但是的;)

答: 暂无答案