我試圖從.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();
}
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;}
}
我做到了!感謝伊戈爾。該程序工作如何我需要它! – Svanhildr