简体   繁体   English

为什么我没有得到匹配的 function 来电

[英]Why am I getting no matching function for call to

I am getting these errors:我收到这些错误:

no matching function for call to 'Date::Date()'
Appointment(){

and

no matching function for call to 'Time::Time()'
Appointment(){

Appointment.h约会.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时间.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日期.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    

Here is the main program:这是主程序:

/*
 * 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;
            }
        }

    }
}

I assumed that Appointment.h would inherit the public constructors from Date and Time and pass them on to its own constructor Appointment() .我假设Appointment.h会从DateTime继承公共构造函数,并将它们传递给它自己的构造函数Appointment()

What do I need to change to make it work?我需要更改什么才能使其正常工作? Please include an example in your answer, it would be much appreciated.请在您的答案中包含一个示例,我们将不胜感激。 If you have any questions or noticed anything else, please let me know.如果您有任何问题或注意到其他任何问题,请告诉我。

You assumed wrong, constructors are not inherited in C++.你假设错了,C++ 中没有继承构造函数。 Here's what you need to do这是你需要做的

Appointment::Appointment(int month, int day, int year, int hour, int minute, int howLong) : 
     Date(month, day, year), Time(hour, minute), howlong(howlong)
{
}

If the : syntax is unfamiliar to you (it seems to be unfamilar to every newbie) then you need to look up initializer lists .如果您不熟悉:语法(每个新手似乎都不熟悉),那么您需要查找初始化列表

Here are some other things you need to fix.这是您需要解决的其他一些问题。 The Date constructor is wrong Date构造函数错误

Date(int, int,int){   
  month;
  day;
  year;
}

that should be那应该是

Date(int m, int d, int y) : month(m), day(d), year(y)
{
}

The Time constructor is wrong in the same way Time构造函数以同样的方式错误

Time(int, int){   
    hour;
    minute;
}

that should be那应该是

Time(int h, int m) : hour(h), month(m)
{
}

Most importantly you seem to be making the classic newbie mistake of writing code without testing it.最重要的是,您似乎犯了一个经典的新手错误,即未经测试就编写代码。 You're heading for failure unless you test your code as you go along.除非您像 go 一样测试您的代码,否则您将走向失败。 Write a few lines of code, test it to make sure it's working, then write a few more lines of code.写几行代码,测试它以确保它工作,然后再写几行代码。

The way you are going now, you will end up with 100 lines of code with a dozen errors, and then you will be completely stuck.按照你现在的方式,你最终会得到 100 行代码和十几个错误,然后你会完全卡住。 There's no way a beginner can fix code with multiple errors because it's impossible to tell whether you are making progress or not.初学者无法修复具有多个错误的代码,因为无法判断您是否正在取得进展。 If you have ten errors and you fix one, the remaining nine errors will still stop your code working, so how will you know whether you are going forwards or backwards.如果你有 10 个错误并且你修复了一个,剩下的 9 个错误仍然会阻止你的代码工作,那么你怎么知道你是前进还是后退。

The errors you made in your Date and Time constructors should have been caught immediately after you have written that code.您在DateTime构造函数中犯的错误应该在您编写该代码后立即被捕获。 Test your code as you go along, I can't stress how important that is.像 go 一样测试您的代码,我无法强调这是多么重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用模板函数会收到“没有匹配的函数来调用&#39;...&#39;”? - Why am I getting “no matching function for call to '…'” with template function? 为什么对于可变参数模板函数,我得到“没有匹配的函数调用 ..”? - Why am I getting "no matching function for call to .." for the variadic template function? 为什么没有匹配的函数调用错误? - Why am I getting no matching function call error? 为什么我会收到“没有匹配的功能”错误? - Why am I getting a "no matching function" error? 为什么我收到 class function 的参数错误? “没有匹配的 function 来电” C++ - why am i getting error for parameters of class function? “no matching function for call” C++ 为什么会出现以下错误:在构造函数&#39;B :: B(int,int)&#39;中:没有匹配的函数可用于调用&#39;A :: A()&#39; - why am I getting the following errors : In constructor 'B::B(int, int)': no matching function for call to 'A::A()' 我收到以下错误:没有匹配的函数来调用&#39;A :: A()&#39;? - I am getting the following Error: no matching function for call to ‘A::A()’? 为什么我收到 [Error] no matching function for call to 'car::car()' - why i m getting [Error] no matching function for call to 'car::car()' 没有匹配的通话功能,但是为什么呢? - No matching function for call, but why? 为什么我收到有关 int 与 unsigned long 不匹配的错误? - Why am I getting errors about int not matching unsigned long?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM