我正在尝试编译我的代码,但不断收到错误消息:


  重新定义'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;
}

最佳答案

您在以下头文件中定义了构造函数:

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


那些看上去很无辜的括号将这个声明变成了一个定义。然后,编译器在后来遇到此构造函数的实际定义时感到非常沮丧。只需删除它们:

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

关于c++ - 重新定义类构造函数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39907640/

10-11 22:35
查看更多