简体   繁体   中英

Why am I getting no matching function for call to

I am getting these errors:

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

and

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

Appointment.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() {
        month;
        day;
        year;
        hour;
        minute;
        howLong;

    }

    virtual void print() {
        cout << howLong << " ";
    }

};

#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, int) {
            hour;
            minute;
        }
    virtual void print() {
        cout << hour << " " << minute << " ";
    }

};
#endif    

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, int, int) {
            month;
            day;
            year;
        }
    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    

Here is the main program:

/*
 * Homework 4  -- UPDATE as needed
 */

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

#include "Appointment.h"

using namespace std;

int main() {
    int month, day, year, hour, minute, howLong;
    void callPrint(Time & TimeOrApptObject) {
        TimeOrApptObject.print();
    }
    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;
            }
        }

    }
}

I assumed that Appointment.h would inherit the public constructors from Date and Time and pass them on to its own constructor Appointment() .

What do I need to change to make it work? Please include an example in your answer, it would be much appreciated. If you have any questions or noticed anything else, please let me know.

You assumed wrong, constructors are not inherited in C++. Here's what you need to do

Appointment::Appointment(int month, int day, int year, int hour, int minute, int howLong) : 
     Date(month, day, year), Time(hour, minute), howlong(howlong)
{
}

If the : syntax is unfamiliar to you (it seems to be unfamilar to every newbie) then you need to look up initializer lists .

Here are some other things you need to fix. The Date constructor is wrong

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

that should be

Date(int m, int d, int y) : month(m), day(d), year(y)
{
}

The Time constructor is wrong in the same way

Time(int, int){   
    hour;
    minute;
}

that should be

Time(int h, int m) : hour(h), month(m)
{
}

Most importantly you seem to be making the classic newbie mistake of writing code without testing it. You're heading for failure unless you test your code as you go along. Write a few lines of code, test it to make sure it's working, then write a few more lines of code.

The way you are going now, you will end up with 100 lines of code with a dozen errors, and then you will be completely stuck. There's no way a beginner can fix code with multiple errors because it's impossible to tell whether you are making progress or not. If you have ten errors and you fix one, the remaining nine errors will still stop your code working, so how will you know whether you are going forwards or backwards.

The errors you made in your Date and Time constructors should have been caught immediately after you have written that code. Test your code as you go along, I can't stress how important that is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM