First time poster, but longtime lurker. I've solved a million other problems with other threads, but this time I'm still stumped.
My program is meant to take an array of dates and be able to change them, add days, and resize the array. It compiles OK, but I get a runtime error. Here's the code:
// **********************************
//DateArr.h
// **********************************
#include <string.h>
#include <iostream>
using namespace std;
class Date
{
protected:
int year;
int month;
int dayInMonth;
int NumDaysInMonth(int m, int y);
public:
// Month should be 1, 2, ... 12
// d should be 1, 2, ... 31
// Depending on the month and year,
// Throw exceptions if bad parameters come in.
Date (int y=2000, int m=1, int d=1);
Date operator + (int days);
friend ostream& operator << (ostream& s, Date & d);
friend class DateArr;
};
class DateArr {
private:
int size;
Date *arr;
public:
DateArr(int size=10);
~DateArr();
Date & operator[] (int pos);
void resize(int newSize);
};
// **********************************
//DateArr.cpp
// **********************************
#include "DateArr.h"
#include <vector>
Date::Date(int y, int m, int d)
{
this->year = y;
this->month = m;
this->dayInMonth = d;
}
Date Date::operator +(int days)
{
Date result = *this;
result.dayInMonth += days;
return result;
}
int Date::NumDaysInMonth(int m, int y)
{
// Assuming m = 1, 2, ... 12
int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (m != 2) return months[m-1];
if ((y % 4 == 0) && (y % 100 != 0))
return 29; // Leap year
if (y % 400 == 0) return 29; // One very special case
return 28;
}
std::ostream& operator << (ostream& s, Date & d)
{
s << "Year: " << d.year << " Month: " << d.month << " Day: " << d.dayInMonth << endl;
return s;
}
DateArr::DateArr(int size)
{
Date* arr;
this->size = size;
arr = new Date[size];
for(int i=0;i<size;i++)
{
Date temp;
arr[i].year = temp.year;
arr[i].month = temp.month;
arr[i].dayInMonth = temp.dayInMonth;
}
}
Date &DateArr::operator [](int pos)
{
return arr[pos];
}
void DateArr::resize(int newSize)
{
size = newSize;
DateArr(size);
}
DateArr::~DateArr()
{
delete[] arr;
}
// **************************************
// main.cpp
// **************************************
#include <iostream>
#include <fstream>
using namespace std;
#include "DateArr.h"
int main()
{
DateArr dates(3);
Date d1(2006, 11, 11);
Date d2(2005, 12, 4);
Date d3(1999, 8, 29);
dates[0] = d1;
dates[1] = d2 + 23;
dates[2] = d3 + 100;
for (int i=0; i < 3; i++)
{
cout << "i=" << i << " value=" << dates[i] << endl;
}
dates.resize(5);
for (int i=0; i < 5; i++)
{
cout << "i=" << i << " resized value=" << dates[i] << endl;
}
// Try out some error cases
try
{
Date derr1(1998, 4, 31);
}
catch (char * err)
{
cout << "Test1: error: " << err << endl;
}
try
{
Date derr1(2000, 2, 28);
cout << "1900 should not be a leap year" << endl;
Date derr2(1900, 2, 29);
}
catch (char * err)
{
cout << "Test2 error: " << err << endl;
}
return 0;
}
My error happens in main, at
dates[0] = d1;
Upon researching this, I'm assuming that I'm initializing my array wrongly or I'm going out of bounds. I'm new to pointers, so am I passing an address as a value? The concept is sort of confusing to me.
Debug for the date array (arr) gives me size =3 (which it should be) and arr = 0xcccccccc . Am I right in assuming that arr should be a different value?
Thanks in advance, I'm lost.