我有一個相當令人沮喪的問題,我試圖將指針數組的內容填充到另一個類,以便可以打印數組的內容但不在改變我傳遞數組的方式時,我試圖做的事情總是會出現分段錯誤。我已經查找了所有遇到類似問題的人,但我沒有找到任何具有相同問題的人,但如果這是重複的問題,我仍然表示歉意! 反正,我的這兩個類代碼是傳遞類指針數組時分段錯誤
#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)
後發生
請發佈重現錯誤的完整程序。 http://stackoverflow.com/help/mcve – xaxxon
'printEvent(Event e)'添加返回類型 – DimChtz
雖然在早些時候搞亂了程序,但我必須刪除返回類型並忘記將其添加回去......無論我還在得到一個返回類型的分段錯誤,它也發生在我的deleteEvent函數上,這導致我相信(也許完全不正確?)這個問題與我傳遞我的信息的方式有關 – John