1

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.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
MR Hones
  • 61
  • 8
  • 1
    You might want to talk to Pokmons222. Looks like they stole your code: https://stackoverflow.com/questions/63289666/multiple-inheritance-expected-class-before . Watch out for `while (!HW4DataFileHandle.eof())`. [It's a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Aug 06 '20 at 20:18
  • Dropping the same comment I made at Pokmons222's question: Inheritance implies an [is-a relationship](https://en.wikipedia.org/wiki/Is-a), so `Appointment` is a `Date` and a `Time`, and this [seems conceptually wrong](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle). More likely you you want a [has-a relationship](https://en.wikipedia.org/wiki/Has-a). – user4581301 Aug 06 '20 at 20:21

1 Answers1

1

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.

john
  • 71,156
  • 4
  • 49
  • 68
  • I made the change you sugested and now I am getting the error extra qualification 'Appointment::' on member 'Appointment' [-fpermissive]. – MR Hones Aug 06 '20 at 20:20
  • @MRHones You have declared and defined the constructor inside the class definition, so you don't need to specify the scope of the function. Remove the `Appointment::`. When you separate the class definition and the function definitions you need to tell the compiler what class the function belongs to, and that's what John has done here. – user4581301 Aug 06 '20 at 20:23
  • Yeah I just did that fixed the problem – MR Hones Aug 06 '20 at 20:27