运算符重载函数无法访问私有成员

Operator- overloading function cannot acess private members

本文关键字:成员 访问 重载 函数 运算符      更新时间:2023-10-16

我想要一个重载函数,它从一个 Date 对象中减去另一个对象并返回以天为单位的差异。问题是我的重载函数对所有私有变量完全视而不见。

我试图让它返回一个 Date 对象,但没有成功。

这是我的.h文件。

#pragma once
#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int day;
int month;
int year;
const int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public:
Date();
// There are other functions here. That one below is what matters, I guess.
friend int operator - (const Date&, const Date&);
};

下面是我的Date.cpp文件的一些功能。

Date::Date()
{
day = 1;
month = 1;
year = 2000;
}
void Date::setDate()
{
cout << "Month: ";
cin >> month;
while (month < 1 || month > 12)
{
cout << "Invalid month. Try again: ";
cin >> month;
}
cout << "Day: ";
cin >> day;
while (day < 1 || day > monthDays[month - 1])
{
cout << "Invalid day. Try again: ";
cin >> day;
}
cout << "Year: ";
cin >> year;
}

构造函数可以毫无问题地访问 monthDays 数组。但关于操作员,这不能这样说——:

int operator-(const Date& left, const Date& right)
{
int differenceDays = 0, oprTemp;
// Checks the year.
oprTemp = left.year - right.year;
if (oprTemp >= 0)
differenceDays += oprTemp * 365;
else
return -1;
// Checks the months.
oprTemp = left.month - right.month;
if (oprTemp >= 0)
{
for (int i = 0; i < oprTemp; i++)
differenceDays += monthDays[left.month - 1 - i];
}
else
return -1;
// Checks the day.
oprTemp = left.day - right.day;
if (oprTemp > 0)
differenceDays += oprTemp;
return differenceDays;
}

不要打扰上面的函数逻辑。由于明显的原因,它还没有经过测试。:)

我需要的是一个重载的函数来返回两个 Date 对象之间的差异,并以整数形式返回以天为单位的差异。如果存在错误数据,则返回 -1。

非常感谢您的口吻,并为我的英语感到抱歉。 谢谢。

这是由于monthDays是Date的非静态成员,因此它需要一个对象才能访问。 由于运算符-(..(是友元函数,因此没有this。您可以将monthDays声明为 static 并使用Date::monthDays或使用leftrightmonthDays成员。由于monthDays在实例之间不会更改,因此与任何对象一起使用都应该没问题。

class Date
{
private:
int day;
int month;
int year;
const int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public:
Date();
void setDate();
// There are other functions here. That one below is what matters, I guess.
friend int operator - (const Date&, const Date&);
};
Date::Date()
{
day = 1;
month = 1;
year = 2000;
}

void Date::setDate()
{
cout << "Month: ";
cin >> month;
while (month < 1 || month > 12)
{
cout << "Invalid month. Try again: ";
cin >> month;
}
cout << "Day: ";
cin >> day;
while (day < 1 || day > monthDays[month - 1])
{
cout << "Invalid day. Try again: ";
cin >> day;
}
cout << "Year: ";
cin >> year;
}
int operator-(const Date& left, const Date& right)
{
int differenceDays = 0, oprTemp;
// Checks the year.
oprTemp = left.year - right.year;
if (oprTemp >= 0)
differenceDays += oprTemp * 365;
else
return -1;
// Checks the months.
oprTemp = left.month - right.month;
if (oprTemp >= 0)
{
for (int i = 0; i < oprTemp; i++)
differenceDays += left.monthDays[left.month - 1 - i];
}
else
return -1;
// Checks the day.
oprTemp = left.day - right.day;
if (oprTemp > 0)
differenceDays += oprTemp;
return differenceDays;
}

您应该将monthDays值和运算符都设置为静态,因为它们都不需要引用实例成员。运算符引用实例成员,但只能通过其参数引用。运算符必须将数组引用为Date::monthDays。你也可以将monthDays声明为constexpr,因为它可以在编译时计算。

#pragma once
#include <iostream>
#include <string>
class Date {
private:
int day;
int month;
int year;
static constexpr int monthDays[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public:
Date();
// There are other functions here. That one below is what matters, I guess.
static friend int operator - (const Date&, const Date&);
};