重新定义类构造函数错误

时间:2016-10-07 00:33:32

标签: c++

我正在尝试编译我的代码而且我一直收到错误:

  

重新定义'SimpleDate :: SimpleDate(int,int,int)

我认为错误通常是因为没有添加#ifndef#define#endif,但我确实添加了这些错误。

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;
}

1 个答案:

答案 0 :(得分:1)

您在头文件中定义了构造函数,此处为:

'local' => [
     'driver' => 'local',
     'root'   => public_path('web/photos'),
],

那些无辜的大括号将这个宣言变成了一个定义。然后编译器在后来遇到这个构造函数的实际定义时非常沮丧。只需删除它们:

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