【问题标题】:Base Class Pointer to derived class inherited function call指向派生类继承函数调用的基类指针
【发布时间】:2013-11-21 00:50:00
【问题描述】:

我正在尝试从 .txt 文件中读取数据,然后创建一个新对象作为基类指针以存储在向量中。我读入了数据,一切都正确,但是当我将它传递给派生类的构造函数(它也传递给基类默认构造函数)时,它会抛出 badptr 和一个随机数。我不知道我哪里出了问题,并且在这一点上花了几个小时试图调试它。我将不胜感激任何帮助试图弄清楚它。

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

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

这是我的代码:

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

约会.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;}
}

【问题讨论】:

  • 我做到了!感谢伊戈尔。该程序按我的需要运行!

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


【解决方案1】:

您的代码根本不像您认为的那样。以此为例:

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

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

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

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


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

【讨论】:

  • 我现在觉得很可笑,我一直在想类似的事情,但没有发现......谢谢。
猜你喜欢
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
相关资源
最近更新 更多