默认构造函数错误,没有用于调用的匹配函数

时间:2020-08-06 21:33:50

标签: c++ class

main()中,我正在制作一个数组myAppointments,并意识到要使其工作,我需要在Appointments.h中创建一个默认构造函数,但是当我得到这个错误时:

no matching function for call to 'Time::Time()'
Appointment(){

这是主要的:

/*
 * Homework 4  -- UPDATE as needed
*/ 
    
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
    
#include "Appointment.h"
using namespace std;
    
void callPrint (Time &TimeOrApptObject) { TimeOrApptObject.print(); }

int main(){
    int month, day, year, hour, minute,howLong;
    Appointment  myAppointments[19];
    
    ifstream HW4DataFileHandle;
    
    HW4DataFileHandle.open("Lab6Data.txt");
    while (!HW4DataFileHandle.eof( )) {
        for (int i = 1; i < 20; i++) {
            HW4DataFileHandle>>month;
            HW4DataFileHandle>>day;
            HW4DataFileHandle>>year;
            HW4DataFileHandle>>hour;
            HW4DataFileHandle>>minute;
            HW4DataFileHandle>>howLong;
            myAppointments[i] = Appointment( month, day, year, hour, minute, howLong);
        }
        cout <<"enter a month" <<endl;
        cin >> month;
        cout <<"enter a day" <<endl;
        cin >> day;
        cout <<"enter a year"<<endl;
        cin >> year;
        Date myDate( month, day, year);
    
        cout <<"Appointments for" << month <<"/" << day <<"/" << year <<":"<< endl;
    
        for (int i = 0; i <13; i++){
            if ( myAppointments[i]==Date myDate )
            {
                Time thisTime = myAppointments[i];
                thisDate.print();
                cout << endl;
            } 
        }
    }

这是头文件:

Date.H

// Date.h -- Class Date    UPDATE  as needed
#ifndef DATE_H
#define DATE_H

class Date  {  
private:
    int month;
    int day;
    int year;
public:
    Date(int m, int d, int y) : month(m), day(d), year(y)
    {
    }

    friend bool friendTorCompare2Dates (const Date&,const Date& );
};

bool friendTorCompare2Dates (const Date& Right, const Date& Left)
{
    if (Right.month == Left.month && Right.day == Left.day )
        return true;
    else
        return false;
}
    
#endif

Time.H

//Time.h -- Class Time UPDATE  as needed
using namespace std;
#include<iostream>
#ifndef TIME_H
#define TIME_H

class Time {
private :
    int hour; int minute;
public:
    Time(int h, int m) : hour(h)
    {
    }
    virtual void print() {
        cout << hour << " " << minute <<" "  ;
    }
};

#endif

约会。h

// Appointment.h -- Class Appointment   UPDATE as needed
//
    
using namespace std;
#include "Time.h"
#include "Date.h"
#ifndef APPOINTMENT_H
#define APPOINTMENT_H

class Appointment:  public Date, public Time {  
private:
    int howLong;
public:
    Appointment(int month, int day, int year, int hour, int minute, int howLong) : 
        Date(month, day, year), Time(hour, minute), howLong(howLong)
    {
    }

    Appointment(){
        month;
        day;
        year;
        hour;
        minute;
        howLong;    
    }
};
        
#endif

要使Appointment中的默认构造函数起作用,我需要更改什么?如果您发现其他任何问题,或者对我的问题有任何其他疑问,请告诉我。如果您在回答中加入一个例子,我将不胜感激。

2 个答案:

答案 0 :(得分:4)

此行:

Appointment  myAppointments[19];

将为每个元素调用Appointment的默认构造函数。

您的默认构造函数没有显式调用Time的构造函数,因此将调用Time的默认构造函数,该默认构造函数不存在。与Date相同。

您可以像这样恢复默认的构造函数:

class Time {
 // ...
 public:
  Time() = default;
};

要获得合理的默认行为,您应为班级成员提供默认的初始值,例如

class Time {
 private :
   int hour = 0; 
   int minute = 0;
public:
   // ...
};

答案 1 :(得分:1)

声明数组Appointment myAppointments[19];时,Appointment的默认构造函数将立即用于所有19个元素。但是,DateTime没有定义默认构造函数,但是Appointment的默认构造函数将尝试隐式调用它们,因此,由于它们不存在,您将看到错误。

因此,您有4种不同的解决方法:

  1. 显式地使Appointment的默认构造函数调用DateTime的非默认构造函数(就像您的其他Appointment构造函数一样) ,将默认值传递给他们:

    Appointment() : 
        Date(0, 0, 0), Time(0, 0), howLong(0)
    {
    }
    
  2. 赋予DateTime自己的默认构造函数,例如:

    class Date  {  
    private:
        int month;
        int day;
        int year;
    public:
        Date() : month(0), day(0), year(0)
        {
        }
    
        Date(int m, int d, int y) : month(m), day(d), year(y)
        {
        }
    
        ...
    };
    
    class Time {
    private :
        int hour;
        int minute;
    public:
        Time() : hour(0), minute(0)
        {
        }
    
        Time(int h, int m) : hour(h), minute(m)
        {
        }
    
        ...
    };
    

    或者:

    class Date  {  
    private:
        int month;
        int day;
        int year;
    public:
        Date(int m = 0, int d = 0, int y = 0) : month(m), day(d), year(y)
        {
        }
    
        ...
    };
    
    class Time {
    private :
        int hour;
        int minute;
    public:
        Time(int h = 0, int m = 0) : hour(h), minute(m)
        {
        }
    
        ...
    };
    

    或者(仅C ++ 11和更高版本):

    class Date  {  
    private:
        int month = 0;
        int day = 0;
        int year = 0;
    public:
        Date() = default;
        // or:
        // Date() : Date(0, 0, 0) {}
    
        Date(int m, int d, int y) : month(m), day(d), year(y)
        {
        }
    
        ...
    };
    
    class Time {
    private :
        int hour = 0;
        int minute = 0;
    public:
        Time() = default;
        // or:
        // Time() : Time(0, 0) {}
    
        Time(int h, int m) : hour(h), minute(m)
        {
        }
    
        ...
    };
    
  3. 使用placement-new显式构造数组元素,因此您可以在它们上使用Appointment的非默认构造函数,例如:

    typedef unsigned char AppointmentBuf[sizeof(Appointment)];
    // better:
    // #include <type_traits>
    // using AppointmentBuf = std::aligned_storage<sizeof(Appointment), alignof(Appointment)>::type;
    
    int main(){
        AppointmentBuf myAppointments[19];
    
        ...
    
        for (int i = 0; i < 19; i++) {
            ...
            new (&myAppointments[i]) Appointment(month, day, year, hour, minute, howLong);
        }
    
        ...
    
        for (int i = 0; i < 19; i++) {
            Appointment &appt = reinterpret_cast<Appointment&>(myAppointments[i]);
            // use appt as needed...
        }
    
        ...
    
        // cleanup
        for (int i = 0; i < 19; i++) {
            reinterpret_cast<Appointment&>(myAppointments[i]).~Appointment();
        }
    
        return 0;
    }
    
  4. 更改数组以容纳Appointment*指针,然后使用Appointment的非默认构造函数通过new动态构造数组元素,例如:

    int main(){
        Appointment* myAppointments[19] = {};
    
        ...
    
        for (int i = 0; i < 19; i++) {
            ...
            myAppointments[i] = new Appointment(month, day, year, hour, minute, howLong);
        }
    
        ...
    
        for (int i = 0; i < 19; i++) {
            // use myAppointments[i] as needed...
        }
    
        ...
    
        // cleanup
        for (int i = 0; i < 19; i++) {
            delete myAppointments[i];
        }
    
        return 0;
    }
    

    或者(仅C ++ 11或更高版本):

    #include <memory>
    
    int main(){
        std::unique_ptr<Appointment> myAppointments[19];
    
        ...
    
        for (int i = 0; i < 19; i++) {
            ...
            myAppointments[i] = std::make_unique<Appointment>(month, day, year, hour, minute, howLong);
        }
    
        ...
    
        for (int i = 0; i < 19; i++) {
            // use myAppointments[i] as needed...
        }
    
        ...
    
        // no manual cleanup needed
    
        return 0;
    }