NeoFahrenheit
NeoFahrenheit

Reputation: 422

Operator- overloading function cannot acess private members

I want to have a overloaded- function that substracts one Date object from another and return the difference in days. The problem is that my overloaded function is completely blind to all it's private variables.

I have tried to make it returns a Date object instead, with no sucess.

This is my .h file.

#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&);
};

Below there is some function of my Date.cpp file.

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;
}

The constructor can acess the monthDays array with no problem. But that can't be said about the operator-:

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;
}

Don't bother with the above function logic. It hans't been tested yet due to obvious reasons. :)

What I need is a overloaded - function to return the difference bewteen the two Date objects and return, as a integer, that difference in days. If there is bad data, return -1.

Thanks so much for your patitence and sorry for my english. Thank you.

Upvotes: 2

Views: 93

Answers (2)

Scott Hutchinson
Scott Hutchinson

Reputation: 1721

You should make both the monthDays value and the operator static, since neither of them need to reference instance members. The operator references instance members, but only through its parameters. The operator must reference the array as Date::monthDays. Also you can declare monthDays as constexpr, since it can be evaluated at compile time.

#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&);
};

Upvotes: 1

Mohit
Mohit

Reputation: 337

This is due to monthDays being a non-static member of Date, so it requires an object to access. Since operator-(..) is a friend function, there is no this. You could either declare monthDays static and use Date::monthDays or use the monthDays member of left or right. Because monthDays does not change between instances, it should be fine to use with any object.

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;
}

Upvotes: 2

Related Questions