#include <iostream>
#include <string>
#include <sstream>
#include "DayOfYear.h"
usingnamespace std;
void DayOfYear::setMonth(string mon)
{
month = mon;
}
int DayOfYear::calcNum() const
{
setMonth("March"); //Why do you hate me
int num = 0;
/*
int count = 0;
string comp = strMonths[count];
string input;
while(month != strMonths[count])
{
count++;
if(count >11)
{
cout << "The month you entered doesn't exist\nPlease reenter: ";
getline(cin, input);
setMonth(input);
count = 0;
}
}
cout << "count is : " << count << endl;
*/
return num;
}
When I call setMonth, I get a compile time error of:
'DayOfYear::setMonth' : cannot convert 'this' pointer from 'const DayOfYear' to 'DayOfYear &'
Most of the fixes I saw for this error meant making your function const but setMonth cannot be const because it's changing the value of the month field. What am I doing wrong?
DayOfYear::calcNum() is a method that may take a const DayOfYear as implicit parameter for the this pointer, but DayOfYear::setMonth() may not. You'll have to make DayOfYear::calcNum() non-const.
That is because the calcNum function is not telling the truth. It says it
is a const function and won't change the object, but it trying to cheat by calling
the function setMonth function which can change the object.
In other words, calcnum was called with the this pointer pointing
to a constant DayOfYear object, but setMonth needs a pointer to a non constant DayOfYear object - hence the error.