Justin Huey
Justin Huey

Reputation: 1

Redefinition of class constructor error

I am trying to compile my code and I keep getting the error:

redefinition of 'SimpleDate::SimpleDate(int, int, int)

I think the error is usually because of not adding #ifndef, #define, and #endif but I did add those.

simple_date.h:

#ifndef SIMPLE_DATE_H
#define SIMPLE_DATE_H
#include <string>

class SimpleDate
{
  int _year, _month, _day;

public:
  // create a new 'SimpleDate' object with the given year, month, and day
  // throws an invalid_argument error if the date is invalid
  SimpleDate(int year, int month, int day) {};
  // returns the current year
  int year() const { return 0; };
  // returns the current month
  int month() const { return 0; };
  // returns the current day of the month
  int day() const { return 0; };
  // return string formatted as year-month-day with day an month 0 prefixed
  // i.e. 2000-01-01 is Jan 01, 2000
  std::string to_string() const { return ""; };
  // comparison operators
  bool operator==(const SimpleDate& lhs) const { return true; };
  bool operator!=(const SimpleDate& lhs) const { return true; };
  bool operator> (const SimpleDate& lhs) const { return true; };
  bool operator< (const SimpleDate& lhs) const { return true; };
  bool operator>=(const SimpleDate& lhs) const { return true; };
  bool operator<=(const SimpleDate& lhs) const { return true; };
  // returns 'true' if current year is a leap year
  bool is_leap() const { return true; };
  // returns the day of the week; 0 = Sunday
  // HINT: To calculate the day of the week, you need to have a known day. Use
  // 1970-01-01 which was a Thursday. Make sure to reference where you found
  // the algorithm or how you came up with it.
  int wday() const { return 0; };
  // returns the day of the year; Jan 1st = 1
  int yday() const { return 0; };
  // add one day to current date
  SimpleDate& incr_day() { SimpleDate dd {2016, 1, 1}; };
  SimpleDate& operator++() { SimpleDate dd {2016, 1, 1}; };
  // add n day(s) to current date
  SimpleDate& operator+=(int n) { SimpleDate dd {2016, 1, 1}; };
  // subtract one day from current date
  SimpleDate& decr_day() { SimpleDate dd {2016, 1, 1}; };
  SimpleDate& operator--() { SimpleDate dd {2016, 1, 1}; };
  // subtract n day(s) from current date
  SimpleDate& operator-=(int n) { SimpleDate dd {2016, 1, 1}; };
  // add one month to current date
  SimpleDate& incr_month() { SimpleDate dd {2016, 1, 1}; };
  // subtract one month from current date
  SimpleDate& decr_month() { SimpleDate dd {2016, 1, 1}; };
  // add one year to current date
  SimpleDate& incr_year() { SimpleDate dd {2016, 1, 1}; };
  // subtract one year from current date
  SimpleDate& decr_year() { SimpleDate dd {2016, 1, 1}; };
  // returns 'true' if current date is a Monday
  bool is_monday() const { return true; };
  // returns 'true' if current date is a Tuesday
  bool is_tuesday() const { return true; };
  // returns 'true' if current date is a Wednesday
  bool is_wednesday() const { return true; };
  // returns 'true' if current date is a Thursday
  bool is_thursday() const { return true; };
  // returns 'true' if current date is a Friday
  bool is_friday() const { return true; };
  // returns 'true' if current date is a Saturday
  bool is_saturday() const { return true; };
  // returns 'true' if current date is a Sunday
  bool is_sunday() const { return true; };
    };

    #endif

simple_date.cpp:

#include "simple_date.h"


SimpleDate::SimpleDate(int year, int month, int day)
{
    std::cout  >> year;
    if (year < 1970 || year > 2020)
    {
        throw invalid_argument("Don't care about years less than 1970 or greater than 2020");
    }
    else
    {
        _year = year;
    }
    if (month < 1 || month > 12)
    {
        throw invalid_argument("Not a real month");
    }
    else
    {
        _month = month;
    }
    if (day < 1 || day>31)
    {
        throw invalid_argument("Not a real day");
    }
    else if ((_month == 2 || _month == 4 || _month == 6 || _month == 9 || _month == 11) && day > 30)
    {
        throw invalid_argument("Not a real day for this month");
    }
    else if (_month == 2 && day == 29)
    {
        if (this.is_leap())
            _day = day;
        else
            hrow invalid_argument("Not a real day for this month and year");
    }
    else
        _day = day;
}

Upvotes: 0

Views: 1580

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118330

You defined the constructor in the header file, here:

SimpleDate(int year, int month, int day) {};

Those innocent-looking braces turned this declaration into a definition. And then compiler got very upset when it later encountered the real definition of this constructor. Just remove them:

SimpleDate(int year, int month, int day);

Upvotes: 1

Related Questions