我收到这些错误:
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 -- 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
的公共(public)构造函数,并将它们传递给自己的构造函数Appointment()
。我需要更改使其工作吗?请在您的答案中包含一个示例,将不胜感激。如果您有任何疑问或发现其他任何问题,请告诉我。
最佳答案
您认为错误,构造函数未在C++中继承。这就是你需要做的
Appointment::Appointment(int month, int day, int year, int hour, int minute, int howLong) :
Date(month, day, year), Time(hour, minute), howlong(howlong)
{
}
如果您不熟悉:
语法(似乎对每个新手都不熟悉),则需要查找初始化程序列表。这是您需要修复的其他一些事项。
Date
构造函数错误Date(int, int,int){
month;
day;
year;
}
那应该是Date(int m, int d, int y) : month(m), day(d), year(y)
{
}
Time
构造函数以相同的方式出错Time(int, int){
hour;
minute;
}
那应该是Time(int h, int m) : hour(h), month(m)
{
}
最重要的是,您似乎在编写未经测试的代码时就犯了经典的新手错误。除非您在测试过程中测试代码,否则您将走向失败。编写几行代码,对其进行测试以确保其正常工作,然后再编写几行代码。现在的方式是,您将得到100行代码,并带有十几个错误,然后您将完全陷入困境。初学者无法修复带有多个错误的代码,因为无法判断您是否在进步。如果您有十个错误并且已解决了一个错误,那么其余九个错误仍将使您的代码停止工作,因此您将如何知道前进还是后退。
在编写该代码后,应立即捕获在
Date
和Time
构造函数中所犯的错误。在进行过程中测试您的代码,我无法强调它的重要性。