基类指向派生类继承函数调用的指针
Base Class Pointer to derived class inherited function call
我正试图从.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()
析构函数设为虚拟的,否则以后会有不同的问题。
相关文章:
- 使用共享指针的函数调用,其对象应为 const
- 返回指向对象的指针的函数调用是否为 prvalue?
- 如何调用存储在指向"std::函数"的指针中的 lambda?
- 在结构外部调用指向函数的指针
- VkSurfaceKHR 指针的值在函数调用后更改,无需任何显式赋值
- 使用指向成员函数的指针在另一个类中调用类构造函数
- 指针在函数调用后更改其地址
- 努力将指向成员函数的指针绑定到类模板的T成员,然后在槽中调用
- 对std::函数对象的调用不匹配,该对象是指向成员函数的指针
- C++:这是使用整数变量作为函数调用指针的正确方法吗
- 正确的方法是什么?调用指针到指针到指针内部的函数?
- 参数的混合值,当我调用指针成员函数时
- 使用函数指针时,ESP 未在函数调用中正确保存
- 为什么使用指向函数的指针调用虚函数时不需要指针"this"?
- 函数调用歧义(用户定义的转换和 Derived2Base 指针转换)
- 创建指针时是否没有构造函数调用
- 调用指向成员函数的指针时出错
- 这些函数调用的返回指针的区别是什么?
- 如何通过指向返回另一个函数的函数的指针调用具有默认参数的函数
- 为什么我不能在插入的函数调用中直接使用返回的指针作为参数