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

时间:2013-11-21 00:50:01

标签: c++ visual-c++ inheritance

我正在尝试从.txt文件中读取数据,然后创建一个新的oject作为存储在向量中的基类指针。我读取了数据,并且它都是正确的,但是当我将它传递给派生类的构造函数时(它也会转到基类的默认构造函数),它会抛出badptr和一个随机数。我不知道我在哪里出错,并且此时花了几个小时试图调试它。在尝试解决这个问题时,我感激不尽。

即使在OneTime.cpp的构造函数中,我设置了word =" test"它仍然没有把它放在对象中。我也尝试取出OneTime构造函数,使用约会构造函数并在那里发送变量无济于事。

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

这是我的代码:

Test.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();
}

Appointment.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

Appointment.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;}
}

1 个答案:

答案 0 :(得分:0)

您的代码完全不符合您的想法。以此为例:

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

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

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

然后阅读您最喜欢的C ++教科书中的构造函数初始化列表。以类似的方式修复其余的代码留给读者练习。


哦,让~Appointment()析构函数为虚拟,或者您以后会遇到其他问题。