Simple class won't compile

Joined
Jan 31, 2009
Messages
4
Reaction score
0
Hi, I'm new to C++ and have been trying to learn on my own with a book. I've been tearing my hair out trying to figure out why the following won't compile. I simplified the class to help debug.

Header File:
#ifndef DATE_H
#define DATE_H

class Date
{
Public:
Date(int monthValue, int dayValue, int yearValue);
Date();
int GetMonth();
int GetDay();
int GetYear();
void Input();
bool Set(int m, int d, int y);
static int daysInMonth[13];
Private:
int month;
int day;
int year;
bool LeapYear(int theYear);
};

int Date::daysInMonth[] = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

#endif


.cpp file:
#include <iostream>
#include "date.h"
using namespace std;

Date::Date(int monthValue, int dayValue, int yearValue)
{
bool dateSet = Set(monthValue, dayValue, yearValue);

if (!dateSet)
{
month = 1;
day = 1;
year = 2000;
}
}


int Date::GetMonth()
{
return month;
}

int Date::GetDay()
{
return day;
}

int Date::GetYear()
{
return year;
}

void Date::Input()
{
int m, d, y;
char temp;

cout << "Please enter a date of the form M/D/Y: ";
cin << m << temp << d << temp << y << endl;

bool dateSet = Set(m, d, y);

do
{
cout << "Sorry, that was an invalid date. Please try again: ";
cin << m << temp << d << temp << y << endl;

bool dateSet = Set(m, d, y);

} while (!dateSet);
}

bool Date::Set(int m, int d, int y)
{
if ((m == 2) && LeapYear(y))
m = 0;

if ((m >= 1) && (m <= 12))
if ((d >= 1) && (d <= daysInMonth[m]))
if (y > 0)
{
month = m;
day = d;
year = y;

return true;
}

return false;
}

bool Date::LeapYear(int theYear)
{
bool leap;

if(theYear%400 == 0)
leap = true;
else if(theYear%100 == 0)
leap = false;
else if(theYear%4 == 0)
leap = true;
else
leap = false;

return leap;
}
 
Joined
Jan 31, 2009
Messages
4
Reaction score
0
oops, this didn't include the default constructor definition:

Date::Date()
{
month = 1;
day = 1;
year = 2000;
}
 
Joined
Jan 31, 2009
Messages
4
Reaction score
0
oh, and the error was "type name expected" when it got to "public:" in the .h file.

(and : D making a smiley face was not intentional)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top