![](/img/trans.png)
[英]Why am I getting “no matching function for call to '…'” with template function?
[英]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(){
約會.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)
{
}
最重要的是,您似乎犯了一個經典的新手錯誤,即未經測試就編寫代碼。 除非您像 go 一樣測試您的代碼,否則您將走向失敗。 寫幾行代碼,測試它以確保它工作,然后再寫幾行代碼。
按照你現在的方式,你最終會得到 100 行代碼和十幾個錯誤,然后你會完全卡住。 初學者無法修復具有多個錯誤的代碼,因為無法判斷您是否正在取得進展。 如果你有 10 個錯誤並且你修復了一個,剩下的 9 個錯誤仍然會阻止你的代碼工作,那么你怎么知道你是前進還是后退。
您在Date
和Time
構造函數中犯的錯誤應該在您編寫該代碼后立即被捕獲。 像 go 一樣測試您的代碼,我無法強調這是多么重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.