C++ 不明确的构造函数调用

C++ 不明确的构造函数调用,c++,constructor,C++,Constructor,我试图创建一个简单的date类,但是我在主文件上得到一个错误,它说,“重载date()的调用是不明确的。”我不确定为什么,因为我认为只要我的构造函数有不同的参数,我就可以了。这是我的密码: 头文件: #ifndef DATE_H #define DATE_H using std::string; class Date { public: static const int monthsPerYear = 12; // num of months in a yr Date(int

我试图创建一个简单的date类,但是我在主文件上得到一个错误,它说,“重载date()的调用是不明确的。”我不确定为什么,因为我认为只要我的构造函数有不同的参数,我就可以了。这是我的密码:

头文件:

#ifndef DATE_H
#define DATE_H
using std::string;

class Date
{
public:
    static const int monthsPerYear = 12; // num of months in a yr
    Date(int = 1, int = 1, int = 1900); // default constructor
    Date(); // uses system time to create object
    void print() const; // print date in month/day/year format
    ~Date(); // provided to confirm destruction order
    string getMonth(int month) const; // gets month in text format
private:
    int month; // 1 - 12
    int day; // 1 - 31 
    int year; // any year

    int checkDay(int) const;
};

#endif
.cpp文件

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include "Date.h"
using namespace std;

Date::Date()
{
    time_t seconds = time(NULL);
    struct tm* t = localtime(&seconds);
    month = t->tm_mon;
    day = t->tm_mday;
    year = t->tm_year;
}

Date::Date(int mn, int dy, int yr)
{
    if (mn > 0 && mn <= monthsPerYear)
        month = mn;
    else
    {
        month = 1; // invalid month set to 1
        cout << "Invalid month (" << mn << ") set to 1.\n";
    }

    year = yr; // could validate yr
    day  = checkDay(dy); // validate the day

    // output Date object to show when its constructor is called
    cout << "Date object constructor for date ";
    print();
    cout << endl;
}

void Date::print() const
{
    string str;
    cout << month << '/' << day << '/' << year << '\n';

    // new code for HW2
    cout << setfill('0') << setw(3) << day;  // prints in ddd
    cout << " " << year << '\n';             // yyyy format

    str = getMonth(month);

    // prints in month (full word), day, year
    cout << str << " " << day << ", " << year << '\n';
}
#包括
#包括
#包括
#包括
#包括“Date.h”
使用名称空间std;
日期::日期()
{
时间\u t秒=时间(空);
struct tm*t=本地时间(&s);
月份=t->tm\u mon;
day=t->tm\mday;
年份=t->tm\u年;
}
日期:日期(整数mn,整数dy,整数yr)
{
如果(mn>0&&mn)
它们都是无参数可调用的。不能默认构造,因为如何构造对象不明确

老实说,让这三个参数都带有默认参数没有多大意义。我什么时候可以指定一个参数而不指定其他参数

Date(int = 1, int = 1, int = 1900); // default constructor
Date(); // uses system time to create object

这样做会使类不再简单。可读性严重受损,甚至会出现一个错误,您不应该在其中浪费时间。请删除无用的默认参数或第二个构造函数。

您应该声明两个构造函数:

Date(int day, int month, int year)
{
  this->day = day;
  this->month = month;
  this->year = year;
}
Date(); // uses system time to create object
{
  this->day = 1;
  this->month = 1;
  this->year = 1900;
}

我同意GMan写的。但是,如果您将默认构造函数声明为私有,编译器会抱怨相同的错误。实际上,它试图组装两个“相同的”或相同的方法。您还应该在标题中包含参数名称。虽然这些名称在技术上不是必需的,但无法知道用户应该提供(月、日、年)还是(日、月、年)在不深入源文件的情况下。我也会赞同GMan关于根本不提供默认参数的建议。在什么情况下,有人会想要某个随机的日子,只要是在6月份?我会争辩说,如果简单是目标,他应该删除使用系统时间的表单。设置默认日期会让你更容易We’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’。
Date(int = 1, int = 1, int = 1900); // default constructor
Date(); // uses system time to create object
Date(int day, int month, int year)
{
  this->day = day;
  this->month = month;
  this->year = year;
}
Date(); // uses system time to create object
{
  this->day = 1;
  this->month = 1;
  this->year = 1900;
}