just getting started with classes. Have a Date class, using enumeration to define months. Then I overload operator << to print out date of "today" and get the following error:
The error happened after I defined Month as enum. Before I had it defined among private as an int and all worked well.
[code]
enum class Month{
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date
{
int year; // year
Month m; // month
int day; //day of the month
public:
void add_day(int n);
Date(int y, Month m, int d);
ostream& operator<<(ostream& os, const Date& d);
}
#include <iostream>
enumclass Month{
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date
{
int year; // year
Month m; // month
int day; //day of the month
public:
void add_day(int n);
Date(int y, Month m, int d);
std::ostream& operator<<(std::ostream& os, const Date& d);
};
std::ostream& operator<<(std::ostream& os, const Date& d)
{
os << '(' << d.year << ',' <<d.month()<< ',' << d.day << ')';
return os;
}
int main()
{
Date today = Date(1978, Month::feb, 25);
std::cout << today ;
}
15:59: error: 'std::ostream& Date::operator<<(std::ostream&, const Date&)' must take exactly one argument
In function 'std::ostream& operator<<(std::ostream&, const Date&)':
9:7: error: 'int Date::year' is private
21:18: error: within this context
21:34: error: 'const class Date' has no member named 'month'
11:7: error: 'int Date::day' is private
21:53: error: within this context
Line 15 declares a member function of Date and with wrong number of arguments.
Lines 19-23 declare and define a standalone function; different from line 15.
Perhaps you were trying to declare the standalone as a friend of the class?