c++ - 为什么我没有得到匹配的调用函数
问题描述
我收到这些错误:
调用 '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()
。
我需要更改什么才能使其正常工作?请在您的答案中包含一个示例,我们将不胜感激。如果您有任何问题或注意到其他任何问题,请告诉我。
解决方案
你假设错了,构造函数不是在 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 行代码和十几个错误,然后你会完全卡住。初学者无法修复具有多个错误的代码,因为无法判断您是否正在取得进展。如果你有 10 个错误并且你修复了一个,剩下的 9 个错误仍然会阻止你的代码工作,那么你怎么知道你是前进还是后退。
您在您的Date
和Time
构造函数中犯的错误应该在您编写该代码后立即被捕获。在进行过程中测试您的代码,我无法强调它的重要性。
推荐阅读
- sql - NodeJS 和 MariaDB,查询中的占位符
- html - 如何使用 Safari 从文本节点中删除第一个和最后一个空格?
- javascript - react-testing-library - fireEvent.select() 不工作
- python - 从字符串中删除 "
- python - 使用 Python 检测特定的声音(频率)
- google-app-maker - Google App Maker:如何为绑定到计算数据源的表格小部件添加分页和排序?
- javascript - 如何使用 Vue JavaScript 对元素进行分组以在数组中添加数量
- firebase - 在 Flutter 应用中通过 Firebase 进行身份验证最少需要哪些 Google Play 服务 API?
- javascript - 响应式设计上的灯箱视觉显示错误
- python - Python 3.x turtle.Screen().register_shape() 不工作