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.

Recommended Answers

All 5 Replies

You need to be more careful about your variable names and scoping rules. You generally want to call your member variables something along the lines of "mVarName" or "myVarName" and your functions' local variables (parameters and locally-defined) something else such as "newVarName" or "localVarName".

What is happening is you are masking (hiding) your size and arr member variables from your functions by using identically-named local variables. When you do this, it causes confusion both for you and people that attempt to read your code. The compiler doesn't care, that's why it doesn't catch it for you. It just happily goes about its business unknowingly using the incorrect variable because you told it to use that one. I suggest you modify your variable names according to some convention, similar to what I mentioned, then see what happens.

i've changed my local variables in my DateArr initialization method to this:

DateArr::DateArr(int mySize)
{
	Date* darr;
	size = mySize;
	darr = new Date[size];
	for(int i=0;i<size;i++)
	{
		Date temp;
		darr[i].year = temp.year;
		darr[i].month = temp.month;
		darr[i].dayInMonth = temp.dayInMonth;
	}

i'm sorry about the duplicate variables. this is homework, and I was supplied with the header and main. my job is just to write the dateArr implementation.

i'm still getting my access violation though. did I go wrong with my array subscript overload? i don't see where else the compiler is being confused yet.

again, apologies for the weak code.

The structure of your constructor is good. However, it is only initializing member variable "size" it is not initializing "arr". This is the reason the debugger is reporting the value 0xcccccccc for arr. In VS, that is a common value for an uninitialized pointer.

There is no need whatsoever for the variable "darr" that you declare and use in your constructor. Remove it, and correct the variables that you use. If you correct the variables properly, the program should now run properly.

aha! oh man, thanks for the help. that did the trick for the access violation. i replaced my darr with this->arr and it began to work. the program is still getting errors, but i'm pretty confident i can fix them. thanks for your help!

Should I watch for more questions or is this thread solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.