基类指向派生类继承函数调用的指针

Base Class Pointer to derived class inherited function call

本文关键字:函数调用 指针 继承 派生 基类      更新时间:2023-10-16

我正试图从.txt文件中读取数据,然后创建一个新的oject作为基类指针存储在向量中。我在中读取了数据,所有数据都是正确的,但当我将其传递给派生类的构造函数时(它也会传递给基类默认构造函数),它会抛出badptr和一个随机数。我不知道哪里出了问题,目前我已经花了几个小时试图调试它。如果有任何帮助,我将不胜感激。

即使在OneTime.cpp的构造函数中,我也设置了word="test",但它仍然没有将其放入对象中。我也尝试过去掉OneTime构造函数,使用Appointment构造函数并将变量发送到那里,但没有成功。

我可能做了一些愚蠢的事。。。所以,是的,任何帮助都会很棒。

这是我的代码:

测试.cpp

#include <fstream>
#include "Monthly.h"
#include "Appointment.h"
#include "OneTime.h"
#include "Daily.h"
using namespace std;
void main(){
    vector<Appointment*> appt;
    string type;
    string desc;
    string apm;
    int prio, dayz, mon, year, hour, min;
    ifstream myfile("C:\Users\Computer\Desktop\Problem 2a\ApptData.txt");
    while(!myfile.eof()){
        string temp;
        getline(myfile, type, ':');
        getline(myfile, desc, ':');
        getline(myfile, temp, ':');
        prio = atoi(temp.c_str());
        getline(myfile, temp, ':');
        dayz = atoi(temp.c_str());
        getline(myfile, temp, ':');
        mon = atoi(temp.c_str());
        getline(myfile, temp, ':');
        year = atoi(temp.c_str());
        getline(myfile, temp, ':');
        hour = atoi(temp.c_str());
        getline(myfile, temp, ':');
        min = atoi(temp.c_str());
        getline(myfile, apm, 'n');
        if(type.compare("OneTime") == 0){
            Appointment* tempOneTime = new OneTime(desc, prio, year, mon, dayz, hour, min, apm);
            appt.push_back(tempOneTime);
            cout << "OneTime object created." << endl;
        }
        if(type.compare("Daily") == 0){
            Appointment* tempDaily = new Daily(desc, prio, year, mon, dayz, hour, min, apm);
            appt.push_back(tempDaily);
            cout << "Daily object created." << endl;
        }
        if(type.compare("Monthly") == 0){
            Appointment* tempMonthly = new Monthly(desc, prio, year, mon, dayz, hour, min, apm);
            appt.push_back(tempMonthly);
            cout << "Monthly object created." << endl;
        }
    }
    myfile.close();
}

预约.h

#include <string>
using namespace std;
#ifndef APPOINTMENT_H
#define APPOINTMENT_H
class Appointment{
public:
    Appointment();
    Appointment(string desc, int priority, int year, int mon, int day, int hour, int min, string apm);
    ~Appointment();
    virtual bool occursOn(int year, int mon, int day) = 0;
    virtual void display() const;
protected:
    string desc;
    int priority;
    int year;
    int mon;
    int day;
    int hour;
    string apm;
    int min;
};
#endif

预约.cpp

#include <string>
#include <iostream>
#include "Appointment.h"
using namespace std;
Appointment::Appointment(){
    string desc = "n";
    int priority = 1;
    int hour = 12;
    int min = 0;
    string ampm = "A.M.n";
    int year = 1900;
    int mon = 1;
    int day = 1;
}
Appointment::Appointment(string word, int prio, int years, int month, int days, int hours, int minute, string apm){
    string desc = word;
    int priority = prio;
    int hour = hours;
    int min = minute;
    string ampm = apm;
    int year = years;
    int mon = month;
    int day = days;
}
Appointment::~Appointment(){
}
void Appointment::display() const{
    cout << this->desc << " at " << this->hour << ":" << this->min << " Priority: " << this->priority << endl;
}

OneTime.h

#ifndef ONETIME_H
#define ONETIME_H
#include "Appointment.h"
#include <string>
class OneTime : public Appointment{
public:
    OneTime();
    OneTime(string desc, int priority, int year, int mon, int day, int hour, int min, string apm);
    ~OneTime();
    virtual bool occursOn(int year, int mon, int day);
protected:
    string desc;
    int priority;
    int year;
    int mon;
    int day;
    int hour;
    string apm;
    int min;
};
#endif

OneTime.cpp

#include <iostream>
#include <string>
#include "OneTime.h"
using namespace std;
OneTime::OneTime(){
    string desc = "n";
    int priority = 1;
    int hour = 12;
    int min = 0;
    string ampm = "A.M.n";
    int year = 1900;
    int mon = 1;
    int day = 1;
}
OneTime::OneTime(string word, int prio, int years, int month, int days, int hours, int minute, string apm){
    word = "test";
    cout <<  "Word is equal to " << word;
    string desc = "test";
    int priority = prio;
    int hour = hours;
    int min = minute;
    string ampm = apm;
    int year = years;
    int mon = month;
    int day = days;
}
OneTime::~OneTime(){
}
bool OneTime::occursOn(int useryear, int usermon, int userday){
    if(this->day == userday && this->mon == usermon && this->year == useryear){
        return true;
    }
    else {cout << this->day;return false;}
}

您的代码根本不会做您认为它会做的事情。举个例子:

Appointment::Appointment(){
    string desc = "n";
    int priority = 1;
    // ...
}

这不会初始化类Appointment的数据成员。这声明了一堆局部变量,初始化它们,然后立即丢弃它们。数据成员仍然未初始化。使其

Appointment::Appointment()
    : desc("n"),
      priority(1)
    // ...
{}

然后阅读你最喜欢的C++教材中的构造函数初始值设定项列表。以类似的方式修复代码的其余部分留给读者练习。


哦,把~Appointment()析构函数设为虚拟的,否则以后会有不同的问题。