Couple of errors I can't figure out allocating memory and copy constructor

allocating memory and using copy constructor can't for the life of me figure this out. I think its probably just some petty mistake.

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  
/***************************
*     libraries
****************************/
#include <iostream>
#include <iomanip>
using namespace std;

/****************************************
*     class definitions
****************************************/
class Date
{
    private:
       int  *month;
       int  *day;
       int  *year;

    public:
       // setters
        void setMonth(int m) {*month = m;}
        void setDay(int d) {*day = d;}
        void setYear(int y) {*year = y;}
        

        // getters
        int getMonth() const {return *month;}
        int getDay() const {return *day;}
        int getYear() const {return *year;}
        

       // 'member' functions
        bool calcLeapYear();
        void display();
        int calcDoY();
        // constructors
        Date();
        Date(int, int, int);
		
        Date(Date& obj)
        {
            cout << "*** Copy Constructor ***\n";
            
            month = new int;
            day = new int;
            year = new int;
            
            *month = obj.getMonth();
            *day = obj.getDay();
            *year = obj.getYear();
        }
        
        Date& operator = (Date& obj);
    ~Date();
};
/****************************************
*     member functions for above class
****************************************/
bool Date :: calcLeapYear()
{

    if(getYear() % 400 == 0)
    {
        return true;
    }
    if(getYear() % 100 == 0)
    {
        return false;
    }
    if(getYear() % 4 == 0)
    {
        return true;
    }
    return false;
}

Date :: Date()
{
    month = new int;
    day = new int;
    year = new int;
            
    setMonth(1);
    setDay(1);
    setYear(1900);
    

}

Date :: Date(int m, int d, int y)
{
    month = new int;
    day = new int;
    year = new int;
    
    setMonth(m);
    setDay(d);
    setYear(y);

}

 Date :: Date& operator = (Date& obj) // Date :: Date names the constructer not type, I tried Date Date :: Date& also didnt work 
{
    setMonth(obj.getMonth()); // unable to resolve identifier setMonth
    setDay(obj.getDay()); // unable to resolve identifier setDay
    setYear(obj.getYear()); // unable to resolve identifier setYear
    return *this;
}


Date :: ~Date()
{
    cout << "*** Deconstructor ***\n";
    delete *month; // telling me it expected pointer
    delete *day; // telling me it expected pointer
    delete *year; // // telling me it expected pointer
}

int Date :: calcDoY()
{
    
   int listDays[] = {0,31,59,90,120,131,151,212,242,273,304,334}; 
 
   int answerDay = listDays[getMonth() - 1] + getDay();
   
   if (calcLeapYear() & getMonth() > 2)
   {
           answerDay++;
   
           cout << "The Julian date is " << answerDay << "\n\n";
           
           return answerDay;
   }
                   
   else
   {
       cout << "The Julian date is " << answerDay << endl;
   }
}


void Date :: display()
{
    cout << endl;
    cout << "The month is " << getMonth() << endl;
    cout << "The day is " << getDay() << endl;
    cout << "The year is " << getYear() << endl;

    //bool calcLeapYear(getYear());
    if (calcLeapYear() == true)
    {
        cout << getYear() << " is a leap year" << endl;
    }
    else
    {
        cout << getYear() << " is NOT a leap year" << endl;
    }
}

/*************************************
*     function prototype
*************************************/

//void testDate01();
//void testDate02();
//void testDate03();
void testDate05();
void testDate05a();
void testDate05b();
/************************************
*      global variables
*************************************/

int main()
{
//testDate01();
//testDate02();
//testDate03();
//testDate04();
testDate05();

}

/************************************
*      non-member functions
*************************************/


 /*void testDate01()
{
    cout << "*** Test Date 01 ***\n";
    cout << "*** part 1 ***\n";

        Date isLeapYear;
    int testMonth, testDay, testYear;
    cout << "Enter the month: " << endl;
    cin >> testMonth;
    cout << "Enter the day: " << endl;
    cin >> testDay;
    cout << "Enter the year: " << endl;
    cin >> testYear;

    isLeapYear.setMonth(testMonth);
    isLeapYear.setDay(testDay);
    isLeapYear.setYear(testYear);
    isLeapYear.display();
    cout << endl;

         cout << "*** part 2 ***\n";

     Date d1;
    d1.setMonth(12);
    d1.setDay(1);
    d1.setYear(1994);
    d1.display();

    d1.setYear(1900);
    d1.display();
    d1.setYear(2000);
    d1.display();
    d1.setYear(1995);
    d1.display();
    d1.setYear(1996);
    d1.display();


}

 void testDate02()
{
     cout << "*** Date Test 02 ***\n";
     Date theyear;
     theyear.display(theyear);
    
     
    Date d2(12,25,2000);
    d2.display(d2);
    
    //d2.setDate(12,16,1994); 
    //d2.display();


}

 void testDate03()
 {
   cout << "*** Test Date 03 ***\n";  
   Date d3;
   d3.calcDoY();
   d3.display();
 }*/
 
 
 void testDate05()
 {
    cout << "*** Test Date 05 ***\n";
    
    Date d5;
    testDate05b();
    
    
 }

 void testDate05a()
 {
    cout << "*** Test Date 05a ***\n";
    
    testDate05b();
    
    
 }

  void testDate05b()
 {
    cout << "*** Test Date 05b ***\n";
    Date d501;
    //d501.s = "Jacob";
    d501.display();
    
    
    Date d502(10,31,2015);
    //d502.s = "John";
    d502.display();
    
    
    Date d503 = d501; 
    // Copy default constructor to new object
    // Date 3 <- 1
    d503.display();
    
    system ("PAUSE");
    
    d502 = d501;
    //Overwrite values in Date 2 with Date 1
    //Date 2 <- Date 1
    //destruction of the 
 }
Last edited on
Is there a reason that the private variables in your Date class are pointers instead of "normal" variables.
That's the way he wants it each assignment builds on the last one.
Rather than saying "I just don't get it" when your code doesn't compile, provide the first few complete error messages generated when the compilation fails. Help others help you.

Date::Date& operator = (Date& obj) // Date :: Date names the constructer not type, I tried Date Date :: Date& also didnt work

Date::Date does not name the constructor type. In fact it's kind of nonsensical here. On the other hand, you do need to let the compiler know you're defining Date::operator=:

1
2
3
4
Date& Date::operator=(Date& obj)
{
    // ...
}


If you were concerned about const correctness, obj would be received by const reference.

delete works on pointers. If you have a pointer-to-int such as int* month, the the pointer is month. *month is of type int.

So:
1
2
3
4
5
6
7
Date :: ~Date()
{
    cout << "*** Deconstructor ***\n";
    delete month; 
    delete day; 
    delete year;
}
delete the pointers.
Topic archived. No new replies allowed.