0

I am new to C++ and objected-oriented programming, so please bear with me for this question. I am writing a date program that determines whether the date you input is a leap year, calculates length between two dates, among other things. Here's my code. It has a header file Date.h, and two source files Date.cpp and main.cpp:

Date.h

#include <iostream>
using namespace std;
class Date {
private:
static const int month_nb;
static const unsigned char month_days[];
static const unsigned char month_daysLeap[];
protected:
static bool testLeap(const int year_);
bool isLeap;
int year;
int month;
int day;
public:


Date(const int month_,const int day_,const int year_);


~Date(){};


int get_month() const {return month;}
int get_day() const {return day;}
int get_year() const {return year;}


void set_month(int m){month = m;}
void set_day(int d){day = d;}
void set_year(int y){year = y;}


bool testLeap() const;
void out() const;
bool testValid() const;
int operator-(const Date & date) const;
static int Julian(const Date & date1,const Date & date2);
static double CountACT_365(const Date & date1,const Date & date2);
static int Count30_360L(const Date & date1,const Date & date2);
static double Count30_360(const Date & date1,const Date & date2);
double CountACT_365(const Date & date) const;
int Count30_360L(const Date & date) const;
double Count30_360(const Date & date) const;
};
inline bool operator==(const Date& d1,const Date& d2)
{
return d1.get_month()==d2.get_month()
&& d1.get_day()==d2.get_day() && d1.get_year()==d2.get_year();
}
ostream& operator<<(ostream& os,const Date& d);

Date.cpp

#include <iostream>
#include "Date.h"
using namespace std;
const int Date::month_nb = 12;
const unsigned char Date::month_days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const unsigned char Date::month_daysLeap[] = {31,29,31,30,31,30,31,31,30,31,30,31};
Date::Date(const int month_, const int day_, const int year_)
{
year = year_; month = month_; day = day_;
isLeap = testLeap();
}
bool Date::testLeap(const int year_)
{
//if the year is not a multiple of 4, this is not a leap year
if(year_ % 4 != 0) return false;
//if the year is a multiple of 100, this is not a leap year,
//except if it is a multiple of 400.
if(year_ % 100 == 0) {
return year_ % 400 == 0;
}
return true;
}
bool Date::testLeap() const {
return testLeap(year);
}
void Date::out() const
{
cout << (int)month <<"/"<< (int)day <<"/"<< year << endl;
}
bool Date::testValid() const {
return (1 <= month && month <= month_nb && 1 <= day
&& ((isLeap && day <= month_daysLeap[month-1])
|| (!isLeap && day <= month_days[month-1])));
}
ostream& operator<<(ostream& os,const Date& d) {
return os<<d.get_month()<<'/'<<d.get_day()<<'/'<<d.get_year();
}

main.cpp

 #include <iostream>
#include "Date.h"
using namespace std;
int Count30_360L(Date date1, Date date2) {
return 360 * (int) (date2.get_year() - date1.get_year())
+ 30 * (int) (date2.get_month() - date1.get_month())
+ (date2.get_day() - date1.get_day());
}
double Count30_360(Date date1, Date date2) {
return (double) Count30_360L(date1, date2) / 360.0;
}
int main() {

    Date SBday = Date(8,24,1971);
    Date CBday = Date(9,28,1996);
    cout <<"Time elapsed " << CBday - SBday<<endl;
    cout << "Q: was 8/24/1971 in a leap year? A: "<<SBday.testLeap()<<endl;
    cout <<"Count30_360L(SBday,CBday) = "<< Count30_360L(SBday,CBday)<<endl;
    cout <<"Count30_360(SBday,CBday) = "<< Count30_360(SBday,CBday)<<endl;
    cout <<"CountACT_365(SBday,CBday) = "<< Count30_360L(SBday,CBday)<<endl;
    cout <<"CBday.Count30_360L(SBday) = "<< CBday.Count30_360L(SBday)<<endl;
    cout <<"CBday.Count30_360(SBday) = "<< CBday.Count30_360(SBday)<<endl;
    return 0;
    }

I get the following error messages:

g++ -static-libgcc -static-libstdc++ -o HW1_2.exe main.o Date.o

main.o: In function `main':

...Debug/../main.cpp:24: undefined reference to `Date::operator-(Date const&) const'

...Debug/../main.cpp:29: undefined reference to `Date::Count30_360L(Date const&) const'

...Debug/../main.cpp:30: undefined reference to `Date::Count30_360(Date const&) const'

Could anyone help me out? Thanks!

Patrick Jane
  • 53
  • 1
  • 3
  • 5

1 Answers1

1
  • ...Debug/../main.cpp:24: undefined reference to `Date::operator-(Date const&) const'

You declared int operator-(const Date & date) const; in Date.h but never defined it.

  • ...Debug/../main.cpp:29: undefined reference to `Date::Count30_360L(Date const&) const'

You declared int Count30_360L(const Date & date) const; but did not define it (you did define a free function int Count30_360L(Date date1, Date date2)).

  • ...Debug/../main.cpp:30: undefined reference to `Date::Count30_360(Date const&) const'

Again, you declared double Count30_360(const Date & date) const; in Data.h but never defined it (but did define a free function double Count30_360(Date date1, Date date2)).

The functions are all referenced in main():

  • Bday - SBday
  • CBday.Count30_360L(SBday)
  • CBday.Count30_360(SBday)

You need to define these Date:: member functions (in Date.cpp).

Also:

using namespace std;

This is not a good idea in header files. Please see Why is "using namespace std" considered bad practice?

It would also help hugely with readability were you to indent your code.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    Many thanks for reading over my code despite the poor formatting! I will go ahead and define the functions and fix the other issues. – Patrick Jane Feb 16 '13 at 04:27
  • @PatrickJane: Welcome to StackOverflow! I'm always happy to help those starting out in C++. Most editors will format your code for you, failing that you can use tools like [`astyle`](http://astyle.sourceforge.net/) are worth a look. Good luck (and don't forget [to mark answers that help you as "accepted")](http://stackoverflow.com/faq/#howtoask)! – johnsyweb Feb 16 '13 at 04:53