分段传递类的指针数组时出错

时间:2016-03-25 00:01:47

标签: c++ arrays

我有一个相当令人沮丧的问题,我试图将填充了对象的指针数组的内容传递给另一个类,以便我可以打印数组的内容,但无论我在更改中尝试做什么我通过数组的方式我一直得到一个分段错误。我已经四处寻找有类似问题的人,我没有找到任何有相同问题的人,但如果这是一个重复的问题,我仍然道歉! 无论如何,这两个类的代码是

#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;
        }
    }


}

重要的位是Event *事件[31]; array,displayEvent和PrintEvent函数。我通过引用尝试了所有形式的传递和解引用数组,但似乎没有解决问题... 非常感谢!

编辑:添加了程序的其余部分,在创建新事件(菜单上的选项1)之后发生了分段错误,然后尝试删除它(选项2)或显示它(选项) 3)

1 个答案:

答案 0 :(得分:0)

此功能:

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

在堆栈上创建新的Event e。然后它保存一个指向该局部变量的指针。一旦函数返回,该指针就不再有效并且使用它将导致未定义的行为(例如seg错误,但它可以执行许多其他操作)。

要修复它,请确定这些数组是指针数组还是对象数组。如果你肯定想使用指针,那么你需要弄清楚如何控制它们的生命时间。或者,您可以使用智能指针来为您查看对象的生命周期。

注意:这只是跳出来的第一件事,可能还有其他问题。