As the title states, one of the friend functions I have defined throws an error if I define it after main and call it. It only happens to one of the friend function specifically and the only difference between the problematic friend function and the others is that it takes no arguments.
This code throws an error saying thisYear() was not declared in the scope.
class Date{
private:
int day, month, year;
public:
Date() {day = 1; month = 1; year = 2023;}
Date(int d = 4, int m = 11, int y = 2023){
day = d;
month = m;
year = y;
}
void setYear(int y){year = y;}
void print() const{
cout << day << "/" << month << "/" << year << endl ;
}
friend void nextDay(Date d);
friend void nextMonth(Date &d);
friend void nextYear(Date *d);
friend void thisYear();
};
int main(){
// other code
thisYear();
return 0;
}
void nextDay(Date d) {d.day++;}
void nextMonth(Date &d) {d.month++;}
void nextYear(Date *d) {d->year++;}
void thisYear() {cout << "The current year is 2023" << endl;}
However when I define thisYear() before main, I get the intended output “The current year is 2023”. Why aren’t the other friend functions affected by being defined after main?
Any clarification would be appreciated. Thanks.