为什么我没有得到匹配的函数来调用
Why am I getting no matching function for call to
我收到这些错误:
no matching function for call to 'Date::Date()'
Appointment(){
和
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
这里是主程序:
/*
* 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)
{
}
如果:
语法对你来说很陌生(似乎每个新手都不熟悉)那么你需要查找initializer lists.
您还需要解决一些其他问题。 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
构造函数中犯的错误应该在您编写该代码后立即被发现。在进行过程中测试您的代码,我无法强调它的重要性。
我收到这些错误:
no matching function for call to 'Date::Date()' Appointment(){
和
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
这里是主程序:
/*
* 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)
{
}
如果:
语法对你来说很陌生(似乎每个新手都不熟悉)那么你需要查找initializer lists.
您还需要解决一些其他问题。 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
构造函数中犯的错误应该在您编写该代码后立即被发现。在进行过程中测试您的代码,我无法强调它的重要性。