ostream problem

Hi to every one and sorry for the umpteenth stupid question. I'm working on the drill exercise of the 9 chapter of the Stroustrup c++ book, the book provide a code but, this code don't work on my pc. Substantially I have two files source:

Chrono.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  namespace Chrono
{
    enum class Month {
    jan=1, feb, mar, apr, may, jun, jul, aug, sep, otc, nov, dec
    };
class Date{
public:
    class Invalid{};
    Date(int y, Month m, int d); //check for valid date and initialize
    Date();                      //default constructor


    // non modifying operators
    int day() const{return d;}
    Month month() const {return m;}
    int year() const {return y;}

    //modifying operator
    void add_day(int n);
    void add_month(int n);
    void add_year(int n);

private:
    int y;
    Month m;
    int d;

};

bool is_date(int y, Month m, int d);  //will return true for valid date
bool leapyear(int y);  //will be true for leap year
bool operator ==(const Date& a, const Date& b);
bool operator !=(const Date& a, const Date& b);

ostream& operator <<(ostream& os, const Date& d);
istream& operator >>(istream& is, const Date& dd);

}


and the "main" code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "std_lib_facilities.h"
#include "Chrono.h"

using namespace std;
namespace Chrono
{
Date::Date(int yy, Month mm, int dd)
:y{yy}, m{mm}, d{dd}
{
    if(!is_date(yy,mm,dd))throw Invalid{};
}
const Date& default_date()
{
    static Date dd {2001, Month::jan,1};
    return dd;
}

Date::Date()
    :y{default_date().year()},
    m{default_date().month()},
    d{default_date().day()}
    {}
void Date::add_day(int n)
{
    //...

}
void Date::add_year(int n)
{
  if(m==Month::feb && d==29 && !leapyear(y+n))
    {
        m=Month::mar;
        d=1;
    }
    y+=n;
}

//helper function

bool is_date(int y, Month m, int d)
{
    //assume that y is valid
    if(m<Month::jan || Month::dec<m)return false;
    int days_in_month=31;

    switch(m)
    {
    case Month::feb:
        days_in_month=(leapyear(y))?29:28 ;//arithmetic if
        break;
    case Month::apr: case Month::jun: case Month::sep: case Month::nov:
        days_in_month=30;
        break;
    }
    if (days_in_month<d) return false;
    return true;
}
bool leapyear(int y)
{
    //se ex
}
bool operator==(const Date& a, const Date& b)
{
    return a.year()==b.year()
           && a.month()==b.month()
           && a.day()==b.day();
}
bool operator !=(const Date&a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
    return os<<'('<<d.year()
             <<','<<d.month()
             <<','<<d.day()<<')';
}
istream& operator >>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is>>ch1>>y>>ch2>>m>>ch3>>d>>ch4;
if(!is) return is;
if(ch1!='('||ch2!=','||ch3!=','||ch4!=')')
{
    is.clear(ios_base::failbit);
    return is;
}
dd=Date(y,Month(m),d);
return is;
}
enum class Day{sunday, monday,tusdey,wensday,thursday,friday,saturday};

Day day_of_week(const Date& d)
{
    //..
}

Date next_sunday(const Date& d)
{
    //..
}
Date next_weekday(const Date& d)
{
    //..
}
}

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}



On the line 75 of the last file I have this error:

error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'

I can't see a misspelling in the syntax but I'm a noob, do you have any advise??
My IDE is Code::Block and I have already activated the c++11 feature.
Thank you everyone anyway.





Last edited on
d.month() returns Chrono::Month which does not have output operation defined. You need to convert it to string (or integer depending on what you want) representation first.

EDIT: at page 317 there is an example of output operator for month.
What printing do you use? It is on botom of book info page under ISBN
Last edited on
MiiNiPaa thank you again for the answer, I'd searched on my reference -Stroustrop's book and c++ for dummies- but i didn't find the conversion you stated. can you give the code for only one example?
The strange thing is that this code should work without change since on the book there is usally write if there are typhoos in the code, and this is not the case....
Thank you again.
9_6 wrote:
You might not think that incrementing a Month is common enough to warrant a special operator. That may be so, but how about an output operator? We can define one like this:
1
2
3
4
5
6
vector<string> month_tbl;

ostream& operator<< (ostream& os, Month m)
{
    return os << month_tbl[m];
)

Thlis assumes that month_tbl has been initialized somewhere so that (for example) month_tbl[Mar] is "March" or some other suitable name for that month; see ยง10.11.3.
Thank you again, I've understand that the posted code isn't complete...
Anyway now works.
Many Thanks.
Topic archived. No new replies allowed.