我收到以下错误:
调用"Date::Date(("没有匹配的函数约会((和调用"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
这是主程序:
/* * 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()
。我需要更改什么才能使其工作?请在你的回答中附上一个例子,不胜感激。如果你有任何问题或注意到其他任何事情,请告诉我。
您认为错误,构造函数不是在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
构造函数中所犯的错误应该在编写该代码后立即被捕获。一边测试你的代码,我不能强调这有多重要。