【发布时间】:2020-11-27 03:34:50
【问题描述】:
我收到以下错误:
调用 'Date::Date()' 没有匹配的函数 约定(){和
没有匹配函数调用'Time::Time()' 约定(){约会.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
时间.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
日期.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
这里是主程序:
/*
* 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;
}
}
}
}
我假设Appointment.h 会从Date 和Time 继承公共构造函数并将它们传递给它自己的构造函数Appointment()。
我需要进行哪些更改才能使其正常工作?请在您的答案中包含一个示例,我们将不胜感激。如果您有任何问题或注意到其他任何问题,请告诉我。
【问题讨论】:
-
你可能想和 Pokmons222 谈谈。看起来他们偷了你的代码:stackoverflow.com/questions/63289666/…。注意
while (!HW4DataFileHandle.eof())。 It's a bug -
放弃我在 Pokmons222 的问题中所做的相同评论:继承意味着 is-a relationship,所以
Appointment是Date和Time,而这 seems conceptually wrong。您更有可能想要has-a relationship。
标签: c++ class multiple-inheritance