У меня есть этот класс:
class Date
{
public:
Date(); // Sets a date of January 1, 2000
Date(int mm, int dd, int yyyy); // Sets a date with the passed arguments; automatically converts two-digit years as being AFTER the year 2000
Date after_period_of ( int days ) const; // Elapses an arbitrary amount of time, and returns a new date
string mmddyy () const; // Returns date in MM/DD/YY format (1/1/00)
string full_date () const; // Returns date in full format (January 1, 2000)
string month_name () const; // Returns the name of the month as a string;
private:
int days_in_february(int yr);
int month;
int day;
int year;
};
Когда я пытаюсь передать приватную переменную year
в качестве аргумента в days_in_february
, я получаю следующее сообщение об ошибке:
passing ‘const Date’ as ‘this’ argument of ‘int Date::days_in_february(int)’ discards qualifiers
days_in_february
вызывается в after_period_of
, например:
Date Date::after_period_of (int days_elapsed) const
{
int new_month;
int new_year = year; // tried copying 'year' to get around this issue, but it did not help
int days_into_new_month;
int max_days_in_month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;
max_days_in_month[1] = days_in_february(new_year);
и снова в той же функции:
if (new_month == 13)
{
new_month = 1;
new_year += 1;
max_days_in_month[1] = days_in_february(new_year);
}
days_in_feb February просто возвращает число 28 или 29 в зависимости от года, в который он был передан. Он не пытается манипулировать чем-либо за пределами своего блока.
Я даже пытался передать в нее непрограммированную переменную (days_in_february(2000)
) и получаю ту же ошибку. Я пытался переместить эту функцию в общественное достояние, но это также не решило проблему.
Почему это происходит?
Почему мне не разрешено это делать?