C++ 传递类的指针数组时出现分段错误
我遇到了一个相当令人沮丧的问题,我试图将充满对象的指针数组的内容传递给另一个类,以便打印数组的内容,但无论我尝试如何改变传递数组的方式,我始终会遇到分段错误。我已经到处寻找有类似问题的人,但我没有找到有相同问题的人,但如果这是一个重复的问题,我仍然道歉! 无论如何,我这两个类的代码是C++ 传递类的指针数组时出现分段错误,c++,arrays,C++,Arrays,我遇到了一个相当令人沮丧的问题,我试图将充满对象的指针数组的内容传递给另一个类,以便打印数组的内容,但无论我尝试如何改变传递数组的方式,我始终会遇到分段错误。我已经到处寻找有类似问题的人,但我没有找到有相同问题的人,但如果这是一个重复的问题,我仍然道歉! 无论如何,我这两个类的代码是 #include <iostream> #include <vector> #include <string> using namespace std; //Time Clas
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Time Class
class Time{
public:
Time();
void setHour(int sHour){hour = sHour;}
void setMinute(int sMinute){minute = sMinute;}
void setAmPm(int sAmPm){ampm = sAmPm;}
int getHour(){return hour;}
int getMinute(){return minute;}
int getAmPm(){return ampm;}
private:
int hour;
int minute;
int ampm;
};
//Time Constructor implementation
Time::Time(){
hour = 0;
minute = 0;
ampm = 0;
}
//Event Class
class Event{
public:
Event(string x, Time y);
void setDesc(string sDesc){description = sDesc;}
string getDesc(){return description;}
void setTime(Time t, int h, int m, int ampm){t.setHour(h); t.setMinute(m); t.setAmPm(ampm);}
Time getTime(){return eventTime;}
printEvent(Event e){
Time t = e.getTime();
cout << description << endl;
string ampm ="";
if(t.getAmPm() == 0)
ampm = "AM";
else
ampm = "PM";
cout << t.getHour() << ":" << t.getMinute() << " in the " << ampm;
}
private:
string description;
Time eventTime;
};
//Event Constructor implementation
Event::Event(string cDesc, Time t){
description = cDesc;
eventTime = t;
}
//Month Class
class Month{
public:
Month(string x, int y);
void addEvent(string desc, int h, int m, int ampm, int day){
Time t;
t.setHour(h);
t.setMinute(m);
t.setAmPm(ampm);
Event e(desc, t);
this -> event[day] = &e;
}
void deleteEvent(int day){delete event[day];}
Event getEvent(int day){
return *event[day];
}
void displayEvent(int day){
Event e = getEvent(day);
e.printEvent(e);
}
void displayAll(int days){
// Time t;
// Event e("StupidSolution", t);
// for(int i = 0; i < days; i++)
// e.printEvent(*event[i]);
}
private:
int days;
string month;
Event* event[31];
};
//Month Constructor implementation
Month::Month(string cMonth, int cDays){
days = cDays;
month = cMonth;
}
//both num days and find month are used to determine the Month the user wants to generate a calendar for and the amount of days in that month
string findMonth(int numMonth){
if(numMonth == 1)
return "January";
if(numMonth == 2)
return "February";
if(numMonth == 3)
return "March";
if(numMonth == 4)
return "April";
if(numMonth == 5)
return "May";
if(numMonth == 6)
return "June";
if(numMonth == 7)
return "July";
if(numMonth == 8)
return "August";
if(numMonth == 9)
return "September";
if(numMonth == 10)
return "October";
if(numMonth == 11)
return "November";
if(numMonth == 12)
return "December";
}
int numDays(int numMonth){
if(numMonth == 1)
return 31;
if(numMonth == 2)
return 28;
if(numMonth == 3)
return 31;
if(numMonth == 4)
return 30;
if(numMonth == 5)
return 31;
if(numMonth == 6)
return 30;
if(numMonth == 7)
return 31;
if(numMonth == 8)
return 31;
if(numMonth == 9)
return 30;
if(numMonth == 10)
return 31;
if(numMonth == 11)
return 30;
if(numMonth == 12)
return 31;
}
//gets all necessary info from user for creating a new event
void newEvent(int day, Month month){
string desc = "";
int h = 0;
int m = 0;
int ampm = 0;
cin.get();
cout << "Please enter a Brief Description of the Event!: " << endl;
getline(cin, desc);
cout << "Please enter the Hour of event(1-12): " << endl;
cin >> h;
cout << "Please enter the Minute of event (0-59): " << endl;
cin >> m;
cout << "Please enter 0 if it is in the AM and 1 if it is in the PM: " << endl;
cin >> ampm;
month.addEvent(desc, h, m, ampm, day);
}
int main(void){
string month = "";
int numMonth = 0;
int menuChoice = 0;
int menuLoop = 0;
int days = 0;
int eDay = 0;
cout << "please enter the month you would like to track(1-12): ";
cin >> numMonth;
days = numDays(numMonth);
month = findMonth(numMonth);
Month m(month,days);
while(menuLoop == 0){
cout << "Event Calendar for the month of " << month << endl;
cout << "1. Create a new Event" << endl;
cout << "2. Delete an existing Event" << endl;
cout << "3. Display Event for a particular day" << endl;
cout << "4. Display All Events" << endl;
cout << "5. Exit Program" << endl;
cout << "Enter Choice: " << endl;
cin >> menuChoice;
switch(menuChoice){
case 1:
cout << "What day would you like this event to be on?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
newEvent(eDay, m);
else cout << "Invalid Day";
break;
case 2:
cout << "What day would you like to clear of events?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
m.deleteEvent(eDay);
break;
case 3:
cout << "What day would you like to View?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
m.displayEvent(eDay);
break;
case 4:
cout << "Displaying all Events.";
m.displayAll(days);
break;
case 5:
cout << "Goodbye!" << endl;
menuLoop++;
break;
default:
cout << "incorrect input";
break;
}
}
}
#包括
#包括
#包括
使用名称空间std;
//时间班
上课时间{
公众:
时间();
void setHour(int sHour){hour=sHour;}
void setMinute(int sMinute){minute=sMinute;}
void setAmPm(int-sAmPm){ampm=sAmPm;}
int getHour(){return hour;}
int getMinute(){return minute;}
int getAmPm(){return ampm;}
私人:
整小时;
整数分钟;
int ampm;
};
//时间构造函数实现
时间::时间(){
小时=0;
分钟=0;
ampm=0;
}
//事件类
班级活动{
公众:
事件(字符串x,时间y);
void setDesc(字符串sDesc){description=sDesc;}
字符串getDesc(){return description;}
无效设置时间(时间t,int h,int m,int ampm){t.setHour(h);t.setMinute(m);t.setAmPm(ampm);}
Time getTime(){return eventTime;}
printEvent(事件e){
时间t=e.getTime();
cout此函数:
void addEvent(string desc, int h, int m, int ampm, int day){
Time t;
t.setHour(h);
t.setMinute(m);
t.setAmPm(ampm);
Event e(desc, t);
this -> event[day] = &e;
}
在堆栈上创建一个新的事件
e
。然后保存指向该局部变量的指针。一旦函数返回,该指针将不再有效,使用它将导致未定义的行为(例如seg错误,但它可以执行许多其他操作)
若要解决此问题,请确定这些数组是指针数组还是对象数组。如果您确实想使用指针,则需要确定如何控制指针的生命周期。或者,您可以使用智能指针,为您管理对象的生命周期
注意:这只是我突然想到的第一件事,可能还有其他问题。请发布一个完整的程序来重现错误。printEvent(事件e)
添加返回类型之前在程序中乱搞时,我一定是删除了返回类型,忘记了将其添加回去…尽管如此,我仍然在返回类型中遇到分段错误,并且它也发生在我的deleteEvent函数中,这让我相信(可能完全错误?)这个问题与我传递信息的方式有关。您需要告诉我分段错误是何时发生的。分段错误是在我调用displayEvent函数中的printEvent函数时发生的。谢谢,这听起来似乎正是错误所在。我没有在arra上使用指针数组的具体附件指针的数组是我最先建议的,所以我就这么做了,你认为把指针数组转换成对象数组会有多困难?应该是很简单的。要考虑的是在特定的一天没有事件时发生的事情。对象您需要有另一种检查方法。这可能很简单,只需将说明设置为空白,然后不打印任何带有空白说明的内容。好的,谢谢您的回复!