运算符重载函数无法访问私有成员
Operator- overloading function cannot acess private members
我想要一个重载函数,它从一个 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
或使用left
或right
的monthDays
成员。由于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&);
};
相关文章:
- 这是关于成员访问规则的正确摘要吗
- 为什么我在空指针错误(链表)中获取成员访问权限
- 成员访问是否在空指针上定义C++?
- C++ IDE 不会推断/自动完成对模板类中的 std::array 下标表达式的成员访问
- 为什么类成员数据必须是静态的才能被模板化类的模板化结构成员访问
- 为什么c++允许成员函数定义中实例的私有成员访问
- C/C++ 包含点的宏参数(成员访问运算符)
- 访问说明符(私有/公共/受保护)如何在内部工作(限制成员访问)?
- 如何将超类的受保护成员访问到其派生类. 如果已在派生类中声明了具有相同名称的函数?
- 内部类私有成员访问和封闭的友好性
- 通过 C++ 中的另一个结构成员访问结构
- 具体化 PRVALUES 成员访问的 decltype 行为不正确
- 常量表达式中的静态成员访问
- XVALUE来自类成员访问表达式
- 未经授权的私有类成员访问会产生编译时错误而不是运行时错误?
- 在 c++ 中,为什么 -> 被称为二进制中缀指针成员访问运算符?
- 如何访问模板参数的成员?“成员访问不完整的类型”
- 不明确的可变参数类成员访问
- C 受保护的成员访问
- 将typeID转换为静态成员访问(C )的命名空间