I have a header file and a cpp file that i cannot get to compile. The exercise in the book states i need to make a static char of the months. But for some reason i am having trouble initializing the static char[12][25] array for the 12 months. Getting error codes stating syntax error. Any help would be much helpful....Here is my header file and my main file...

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
using namespace std;

class DayOfYear
{
private:
	static char month[12][25];
	int dayOfYear;

public:
	DayOfYear()
	{	
		dayOfYear = 0;	
		month[12][25]  = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	}
	void setDayOfYear(int doy)
	{ dayOfYear = doy;	}
	void printDayOfYear()
	{
		cout << "Day " << dayOfYear << " would be ";
		if(dayOfYear < 32)
			cout << month[0] << " " << dayOfYear << endl;					//Jan
		else if(dayOfYear < 60)
			cout << month[1] << " " << (dayOfYear) % 31 << endl;		//Feb
		else if(dayOfYear < 91)
			cout << month[2] << " " << (dayOfYear - 27) % 28 << endl;		//March
		else if(dayOfYear < 121)
			cout << month[3] << " " << (dayOfYear - 57) % 31 << endl;		//April
		else if(dayOfYear < 152)
			cout << month[4] << " " << (dayOfYear - 88) % 30 << endl;		//May
		else if(dayOfYear < 182)
			cout << month[5] << " " << (dayOfYear - 118) % 31 << endl;		//June
		else if(dayOfYear < 213)
			cout << month[6] << " " << (dayOfYear - 149) % 30 << endl;		//July
		else if(dayOfYear < 244)
			cout << month[7] << " " << (dayOfYear - 180) % 31 << endl;		//August
		else if(dayOfYear < 274)
			cout << month[8] << " " << (dayOfYear - 212) % 31 << endl;		//Sept
		else if(dayOfYear < 305)
			cout << month[9] << " " << (dayOfYear - 242) % 30 << endl;		//Oct
		else if(dayOfYear < 335)
			cout << month[10] << " " << (dayOfYear - 273) % 31 << endl;		//Nov
		else if(dayOfYear < 366)
			cout << month[11] << " " << (dayOfYear - 304) % 30 << endl;		//Dec
	}
};
#endif
#include <iostream>
#include "DayOfYear.h"
using namespace std;

int main()
{
	DayOfYear doy;

	doy.setDayOfYear(288);

	doy.printDayOfYear();

	return 0;

}

Recommended Answers

All 5 Replies

should be static char *month[12][25]; Chris

Why does it have to be a pointer?

static class member data objects have to be declared outside the class like this: You could do it without the pointers as you posted but that will consume a bit more memory.

// put this in a header file
class DayOfYear
{
protected:
    static char* month[12];
public:
    DayOfYear() {};
};

// put this in a *.cpp file
char* DayOfYear::month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Anyways even with a pointer it still won't compile so I am back to square one.

It compiled for me, post code so we can see what you did. Here is what I tested, which is the same as I posted previously but with main()

#include <iostream>
using namespace std;

class DayOfYear
{
protected:
    static char* month[12];
public:
    DayOfYear() {};
};

char* DayOfYear::month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

int main()
{
}
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.