Date Type

I have to create a Date Type for an online website that can store the date for a cart purchase date so on and so forth, im getting an error with it saying that Date does not name a type and errors saying that constructors dont need base initializers
here is the code
Date.h

namespace Date {
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class invalid { };
Date(int y, Month m, int d);
Date();
int day() const { return d; }
Month month () const { return m; }
int year() const { return y; }
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, Date::Month m, int d);


}

Date.cpp

#include "Date.h"
namespace Date {

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(2012,Date::mar,2);
return dd;
}
Date cart_purchase_date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())

{
}

bool is_date(int y,Date:: Month m, int d)
{
if (d<=0) return false;
if (m<Date::jan || Date::dec<m) return false;
int days_in_month = 31;
switch (m) {
case Date::feb:
days_in_month = 28;
break;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
days_in_month = 30;
break;
}
if (days_in_month<d) return false;
return true;
}
void Date::add_day(int n)
{
d=+1;
}
void Date::add_month(int n)
{
if (m==jan && d==32){
m=feb;
d=1;
}
if (m==feb && d==29){
m=mar;
d=1;
}
if (m==mar && d==32){
m=apr;
d=1;
}
if (m==apr && d==31){
m=may;
d=1;
}
if (m==may && d==32){
m=jun;
d=1;
}
if (m==jun && d==31){
m=jul;
d=1;
}
if (m==aug && d==32){
m=sep;
d=1;
}
if (m==sep && d==31){
m=oct;
d=1;
}
if (m==oct && d==32){
m=nov;
d=1;
}
if (m==nov && d==31){
m=dec;
d=1;
}
if (m==dec && d==32){
m=jan;
d=1;
y=+1;
}
}
}
and Cart.h

/*
The directives starting with # below ensures that this file is read by the compiler only once
even if it is #included several times. It is call an "include guard"
*/
#ifndef CART_H_
#define CART_H_
#include "Date.h"
#include <string>
using namespace std;

struct Cart {
public:
//data

// constructors
Cart () {}

// utility functions
string display() const;
private:
//constants

// private data
int cart_id;
int cust_id;
Date cart_purchase_date;
};


#endif //CART_H_
i have no idea what is going wrong
The Date class is in the Date namespace.


Last edited on
what do you mean the file is Date.h therefor i put namespace Date
feel free to correct me though i have no clue wtf im doing but please offer a way to fix it to not just whats wrong.
Start by correcting these errors
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace Date
{
    class Date
    {
    public:
        enum Month
        {
            jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
        };
        class invalid { };
        Date(int y, Month m, int d);
        Date();
        int day() const
        {
            return d;
        }
        Month month () const
        {
            return m;
        }
        int year() const
        {
            return y;
        }
        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, Date::Month m, int d);
}
#include "Date.h"
namespace Date
{
    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(2012, Date::mar, 2);
        return dd;
    }
    
    //*************************************************
    //What is this next function doing - only constructors have initialisers
    //like you gave here.
    //**ERROR**: only constructors take base initialisers
    Date cart_purchase_date()
            : y(default_date().year()),
            m(default_date().month()),
            d(default_date().day())
    {
    }
    
    
    bool is_date(int y, Date:: Month m, int d)
    {
        if (d <= 0) return false;
        if (m < Date::jan || Date::dec < m) return false;
        int days_in_month = 31;
        //***************************************
        //This switch statement needs to include all possible cases that month can be
        //or you will get compiler Warning
        switch (m)
        {
        case Date::feb:
            days_in_month = 28;
            break;
        case Date::apr:
        case Date::jun:
        case Date::sep:
        case Date::nov:
            days_in_month = 30;
            break;
        }
        if (days_in_month < d) return false;
        return true;
    }
    void Date::add_day(int n)
    {
        d = + 1;
    }
    void Date::add_month(int n)
    {
        if (m == jan && d == 32)
        {
            m = feb;
            d = 1;
        }
        if (m == feb && d == 29)
        {
            m = mar;
            d = 1;
        }
        if (m == mar && d == 32)
        {
            m = apr;
            d = 1;
        }
        if (m == apr && d == 31)
        {
            m = may;
            d = 1;
        }
        if (m == may && d == 32)
        {
            m = jun;
            d = 1;
        }
        if (m == jun && d == 31)
        {
            m = jul;
            d = 1;
        }
        if (m == aug && d == 32)
        {
            m = sep;
            d = 1;
        }
        if (m == sep && d == 31)
        {
            m = oct;
            d = 1;
        }
        if (m == oct && d == 32)
        {
            m = nov;
            d = 1;
        }
        if (m == nov && d == 31)
        {
            m = dec;
            d = 1;
        }
        if (m == dec && d == 32)
        {
            m = jan;
            d = 1;
            y = + 1;
        }
    }
}
/*
The directives starting with # below ensures that this file is read by the compiler only once
even if it is #included several times. It is call an "include guard"
*/
#ifndef CART_H_
#define CART_H_
#include "Date.h"
#include <string>
using namespace std;
struct Cart
{
public:
//data
// constructors
    Cart () {}
// utility functions
    string display() const;
private:
//constants
// private data
    int cart_id;
    int cust_id;
    //********************************
    //We need a name space qualifier for Date class - 
    //it is in the Date namespace 
    //so Date::Date
    Date cart_purchase_date;
};
#endif //CART_H_
int main()
{
    return 0;
}
ok i removed the cart_purchase_date default dates section in date.cpp and added Date::Date under private data in Cart.h and all errors are gone except for its stating that

:: Cart.h:27: error: `Date' does not name a type
> In file included from Menu.h:12,
> from p1_main.cpp:10:
> Cart.h:27: error: `Date' does not name a type
Topic archived. No new replies allowed.