My program compiles OK!
but when I run it I get this error:
undefined reference to DayOfYear::check_date()
undefined reference to DayOfYear::DayOfYear()
//Program to demonstrate the function equal. The class DayOfYear
//is the same as in self-Test Exercise 23-24 in Chapter 10.
#include<iostream>
usingnamespace std;
class DayOfYear
{
public:
DayOfYear(int the_month, int the_day);
//Precondition:the_month and the_day form a
//possible date. Initializes the date according to the arguments
DayOfYear();
//Initializes the date to January first.
void input();
void output();
int get_month();
//Returns the month, 1 for January, 2 for February, etc.
int get_day();
//Returns the day of the month;
private:
void check_date();
int month;
int day;
};
bool equal(DayOfYear date1, DayOfYear date2);
//Precondition: date1 and date2 have values.
//Returns true if date1 and date2 represent the same date;
//otherwise , returns false.
int main()
{
DayOfYear today, bach_birthday(3, 21);
cout << "Enter today's date:\n";
today.input();
cout << "Today's date is: ";
today.output();
cout << "J. S. Bach's birthday is ";
bach_birthday.output();
if ( equal(today, bach_birthday))
cout << "Happy Birthday Johann Sebastian\n";
else
cout << "HappyUnbirthday Johann Sebastian\n";
return 0;
}
bool equal(DayOfYear date1, DayOfYear date2)
{
return (date1.get_month() == date2.get_month() &&
date1.get_day() == date2.get_day() );
}
DayOfYear::DayOfYear(int the_month, int the_day)
: month(the_month), day(the_day)
{
check_date();
}
int DayOfYear::get_month()
{
return month;
}
int DayOfYear::get_day()
{
return day;
}
//Uses iostream
void DayOfYear::input()
{
cout << "Enter the month as a number: ";
cin >> month;
cout << "Enter the day of the month: ";
cin >> day;
}
//Uses iostream
void DayOfYear::output()
{
cout << "month = " << month
<< ", day = " << day << endl;
}
Hehe, it seems I know why. You have defined "check_date()" in the head, but you didn't describe it below. That's why the program first complains about check_date() first and then about the other function although it is declared.
I wonder why the compiler doesn't complain that function is not defined completely, normally you should get a undefined symbol error...
Do you use an IDE? If so, you should be able to get it to automatically make a function stub for each function you declare. Usually there is a menu option to make a new class, this creates a separate header file and cpp file in the project. Once you type a new function declaration in the class, there should be some way of getting the system to make a function stub for it in the cpp file. That way you won't forget to define any of the functions.