提问人:Khaled Bais 提问时间:7/14/2023 更新时间:7/14/2023 访问量:55
在同一文件中使用 bool 类型转换运算符和 Comparison 运算符重载方法
Using bool type conversion operator and Comparison operator overload methods in the same file
问:
我正在尝试执行代码,但每次运行它时,它都使用了错误的运算符:
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 并阅读有关重载方法的信息,但它没有改变
答: 暂无答案
评论
const
bool operator==(const Date& date) const;
Date& date
Date const& date
std::strong_ordering operator<=>(Date const& other) { return daysSince0001_1_1() <=> date.daysSince0001_1_1(); }
operator bool