I'm having trouble returning the string for the month. In the string upDate::getMonthName
, the getMonthName
is underlined and it says Declaration is incompatible with <error-type> upDate getMonthName().
I don't really understand why it doesn't work. For me, I think should be similar the getMonth
, getDay
, getYear
methods, right?
Also, at the very bottom, I don't really understand the ostream
operator. It says that the ostream
is not identified? I know how to do other operating overloads like operator++
and operator=
but I'm kind of confused on how the ostream
one works.
Sorry, I'm just confused about C++ overall :/
#include "upDate.h"
#include <string>
#include <iostream>
using namespace std;
upDate::upDate(){
month = 5;
day = 11;
year = 1959;
}
upDate::upDate(int m, int d, int y) {
if (((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 12) && d > 31) ||
((m == 4 || m == 6 || m == 9 || m == 11) && d > 30 || (m > 12 || m < 1))) {
month = 5;
day = 11;
year = 1959;
}
else if (m == 2 && d >= 29 && y % 4 != 0) {
month = 5;
day = 11;
year = 1959;
}
else {
month = m;
day = d;
year = y;
}
}
void upDate::display() {
switch (month) {
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
}
cout << " " << day << ", " << year;
}
void upDate::setDate(int m, int d, int y) {
if (((m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 12) && d > 31) ||
((m == 4 || m == 6 || m == 9 || m == 11) && d > 30 || (m > 12 || m < 1))) {
month = 5;
day = 11;
year = 1959;
}
else if (m == 2 && d >= 29 && y % 4 != 0) {
month = 5;
day = 11;
year = 1959;
}
else {
month = m;
day = d;
year = y;
}
}
int upDate::getMonth() {
return month;
}
int upDate::getDay() {
return day;
}
int upDate::getYear() {
return year;
}
string upDate::getMonthName(){
string monthN;
switch (month) {
case 1:
monthN = "January";
break;
case 2:
monthN = "February";
break;
case 3:
monthN = "March";
break;
case 4:
monthN = "April";
break;
case 5:
monthN = "May";
break;
case 6:
monthN = "June";
break;
case 7:
monthN = "July";
break;
case 8:
monthN = "August";
break;
case 9:
monthN = "September";
break;
case 10:
monthN = "October";
break;
case 11:
monthN = "November";
break;
case 12:
monthN = "December";
break;
} return monthN;
}
// other methods
ostream &operator<<(ostream &out, upDate &L) {
out << L.getDay() << ", " << L.getYear() << endl; //L.getMonthName() is supposed to be here too
return out;
}
and here is upDate.h
#ifndef MYDATE_H_
#define MYDATE_H_
class upDate {
private:
int month;
int day;
int year;
public:
upDate();
upDate(int, int, int);
void display();
void setDate(int, int, int);
int getMonth();
int getDay();
int getYear();
int convertDateToJulian(int, int, int);
int convertDateToGregorian(int, int&m, int&d, int&y);
string getMonthName();
upDate operator=(upDate);
upDate operator++();
upDate operator++(int);
upDate operator+(int);
friend upDate operator+(int, upDate);
upDate operator--();
upDate operator--(int);
upDate operator-(int);
int operator-(upDate);
friend ostream &operator<<(ostream &out, upDate L);
};
#endif /* MYDATE_H_ */