【发布时间】: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