【问题标题】:Why am I getting no matching function for call to为什么我没有得到匹配的调用函数
【发布时间】:2020-11-27 03:34:50
【问题描述】:

我收到以下错误:

调用 '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 会从DateTime 继承公共构造函数并将它们传递给它自己的构造函数Appointment()

我需要进行哪些更改才能使其正常工作?请在您的答案中包含一个示例,我们将不胜感激。如果您有任何问题或注意到其他任何问题,请告诉我。

【问题讨论】:

标签: c++ class multiple-inheritance


【解决方案1】:

你猜错了,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 个错误仍然会阻止你的代码工作,那么你怎么知道你是前进还是后退。

您在DateTime 构造函数中犯的错误应该在您编写完该代码后立即被捕获。随你测试你的代码,我不能强调这是多么重要。

【讨论】:

  • 我进行了您所建议的更改,现在我在成员 'Appointment' [-fpermissive] 上收到错误额外限定 'Appointment::'。
  • @MRHones 您已经在类定义中声明并定义了构造函数,因此您无需指定函数的范围。删除Appointment::。当你将类定义和函数定义分开时,你需要告诉编译器该函数属于哪个类,这就是 John 在这里所做的。
  • 是的,我刚刚解决了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多