2016-03-25 44 views
-2

我有一個相當令人沮喪的問題,我試圖將指針數組的內容填充到另一個類,以便可以打印數組的內容但不在改變我傳遞數組的方式時,我試圖做的事情總是會出現分段錯誤。我已經查找了所有遇到類似問題的人,但我沒有找到任何具有相同問題的人,但如果這是重複的問題,我仍然表示歉意! 反正,我的這兩個類代碼是傳遞類指針數組時分段錯誤

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


} 

重要比特被事件*事件[31];數組,displayEvent和PrintEvent函數。我已經嘗試過所有形式的傳遞參考和解引用數組,因爲我通過它,但似乎沒有解決問題... 非常感謝!

編輯:添加的程序的其餘部分,該分段錯誤創建一個新的事件(在菜單上的選項1),然後要麼試圖刪除它(選項2)或顯示它(選項3)

後發生
+0

請發佈重現錯誤的完整程序。 http://stackoverflow.com/help/mcve – xaxxon

+0

'printEvent(Event e)'添加返回類型 – DimChtz

+0

雖然在早些時候搞亂了程序,但我必須刪除返回類型並忘記將其添加回去......無論我還在得到一個返回類型的分段錯誤,它也發生在我的deleteEvent函數上,這導致我相信(也許完全不正確?)這個問題與我傳遞我的信息的方式有關 – John

回答

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

創建一個新的Evente堆棧。然後它保存一個指向該局部變量的指針。只要你的函數返回,該指針不再有效,並且使用它將導致未定義的行爲(例如seg錯誤,但它可以做很多其他事情)。

要解決這個問題,找出這些數組是否應該是指針數組或者只是對象數組。如果你一定要使用指針,那麼你需要弄清楚你如何控制自己的生活時間。或者,您可以使用一個智能指針,用於照顧對象的生命期。

注意:這只是我跳出來的第一件事,可能還有其他問題。

+0

謝謝,這聽起來像是錯誤。我沒有具體的附件來使用指向數組的指針數組,指針數組正是我之前建議的,所以我和它一起去了,你認爲將指針數組轉換爲對象數組有多困難? – John

+0

應該很簡單。需要考慮的是當某一天沒有事件時會發生什麼。使用指針,你可以將它設置爲NULL,對象需要另一種檢查方式。這可以像設置'description'爲空白那樣簡單,然後不打印任何空白描述。 –

+0

好的,非常感謝你的知情答覆! – John