IM 收到一個錯誤,指出約會未初始化,但我不知道當我洗掉整個約會部分時該怎么辦,代碼運行良好。我嘗試了一切,但我不知道該怎么做,因為我仍然是初學者,因此感謝您的幫助!謝謝
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
struct Appointment;
struct Time {
int Day, Hour, Minute, Second;
};
struct Employee {
int UniqueID;
string FirstName,LastName, EmailAddress;
Appointment* Calendar;
Employee* next;
Employee* previous;
};
struct Company {
Employee* head, * tail;
};
struct Appointment {
string Title;
Time StartingTime;
int Duration;
Appointment* next;
};
Company* InitializeDoubly() {
Company* c = new Company;
c->head = NULL;
c->tail = NULL;
return c;
}
Appointment* Init() {
return NULL;
}
bool isEmpty(Company *c){
return(c->head == NULL);
}
void InsertAtDoublyTail(Company* c, Employee e) {
Employee* tmp = new Employee;
tmp->UniqueID = e.UniqueID;
tmp->FirstName = e.FirstName;
tmp->LastName = e.LastName;
tmp->EmailAddress = e.EmailAddress;
tmp->Calendar->StartingTime.Day = e.Calendar->StartingTime.Day;
tmp->Calendar->StartingTime.Hour = e.Calendar->StartingTime.Hour;
tmp->Calendar->StartingTime.Minute = e.Calendar->StartingTime.Minute;
tmp->Calendar->StartingTime.Second = e.Calendar->StartingTime.Second;
tmp->Calendar->Duration = e.Calendar->Duration;
tmp->previous = c->tail;
tmp->next = NULL;
c->tail->next = tmp;
c->tail = tmp;
}
void head2tail(Company* c) {
Employee* cur = c->head;
if (isEmpty(c)) {
cout << "LIST IS EMPTY";
return;
}
while (cur != NULL) {
cout << cur->UniqueID << " " << cur->FirstName << " " << cur->LastName << " " <<
cur->EmailAddress << " " << cur->Calendar->Title << " " << cur->Calendar->StartingTime.Day << " "
<< cur->Calendar->StartingTime.Hour<<" " << cur->Calendar->StartingTime.Day <<" "<< cur->Calendar->StartingTime.Second
<<" " << cur->Calendar->Duration<<endl;
cur = cur->next;
}
}
void parsefile(Company *c) {
fstream f("file1.txt", ios::in | ios::out | ios::app);
Employee efile;
string id,day, hour, minute, second;
while (f) {
getline(f, efile.FirstName,'\t');
getline(f, efile.LastName, '\t');
getline(f, efile.EmailAddress, '\t');
getline(f, efile.Calendar->Title, '\t');
getline(f, id, '\t');
getline(f, day, '\t');
getline(f, hour, '\t');
getline(f, minute, '\t');
getline(f, second, '\t');
f >> efile.Calendar->Duration;
efile.UniqueID = stoi(id);
efile.Calendar->StartingTime.Day = stoi(day);
efile.Calendar->StartingTime.Hour = stoi(hour);
efile.Calendar->StartingTime.Minute = stoi(minute);
efile.Calendar->StartingTime.Second = stoi(second);
InsertAtDoublyTail(c, efile);
f.ignore(INT_MAX, '\n');
}
f.close();
}
int main()
{
Company* company1 = InitializeDoubly();
parsefile(company1);
}
也許問題是約會沒有正確初始化,或者應該在主函式中初始化,但我嘗試了它,但我仍然遇到關于 thr 初始化有問題的相同錯誤
uj5u.com熱心網友回復:
在您的Employee結構中,您Appointment* Calendar;的默認設定為 NULL。所以在你的parsefile()函式中,當你這樣做getline(f, efile.Calendar->Title, '\t');是一個明顯的分段錯誤,因為你的efile.Calendar值為 NULL。
此外,使用istringstream來決議字串而不是getline()與分隔符一起使用可能是個好主意。這也顯示在官方檔案here
parsefile像這樣修改你的方法:
void parsefile(Company *c) {
fstream f("file1.txt", ios::in);
Employee efile;
string id,day, hour, minute, second;
string line; // USED TO READ THE ENTIRE LINE
while (getline(f, line)) {
std::istringstream iline(line); // CONVERT IT TO istringstream
string nextWord = ""; // Used to read every word in the line
getline(iline, nextWord, '\t');
efile.FirstName = nextWord; // Set first name
getline(iline, nextWord, '\t');
efile.LastName = nextWord; // Set last name
getline(iline, nextWord, '\t');
efile.EmailAddress = nextWord;
// For the Appointment part, create a temporary Appointment variable similar to efile for Employee
Appointment* temp = new Appointment();
getline(iline, nextWord, '\t');
temp->Title = nextWord; // Set the Title
getline(iline, nextWord, '\t');
efile.UniqueID = stoi(nextWord);
Time t; // Create a temporary time object like efile or temp
getline(iline, nextWord, '\t');
t.Day = stoi(nextWord);
getline(iline, nextWord, '\t');
t.Hour = stoi(nextWord);
getline(iline, nextWord, '\t');
t.Minute = stoi(nextWord);
getline(iline, nextWord, '\t');
t.Second = stoi(nextWord);
temp->StartingTime = t; // Set the Time data member of temp to the temporary object you just constructed.
getline(iline, nextWord, '\t');
temp->Duration = stoi(nextWord); // Set the duration
efile.Calendar = temp; // Now set the Calendar data member to the temporary Appointment* object you just created
InsertAtDoublyTail(c, efile); // Add it to the file
}
f.close();
}
這將確保您在對每個指標執行任何操作之前為正在使用的每個指標分配正確的記憶體。在您中,InsertAtDoublyTail(Company *c, Employee e)您不必對指標內的每個屬性進行硬拷貝。
我在您的代碼中看到的另一個問題是,在您的head2tail方法中,您使用的是起點,Employee* cur = c->head;但在您的代碼中沒有任何地方為c->head. 這意味著c->head它將始終為 NULL,因此實際上不會列印任何內容。
我看到的另一個問題是,在您的 中InsertAtDoublyTail(Company* c, Employee* e),您有:
tmp->previous = c->tail;
tmp->next = NULL;
c->tail->next = tmp;
c->tail = tmp;
對于您添加到公司的第一個員工,c-tail將為 NULL。所以c->tail->next = tmp會導致Segmentation Fault。你需要像這樣修改你的函式:
void InsertAtDoublyTail(Company* c, Employee e) {
Employee* tmp = new Employee(); // Create the employee pointer and set all the values.
tmp->UniqueID = e.UniqueID;
tmp->FirstName = e.FirstName;
tmp->LastName = e.LastName;
tmp->EmailAddress = e.EmailAddress;
tmp->Calendar = e.Calendar;
// Add the employee to company
tmp->previous = c->tail;
if(c->tail == NULL) { // This means that there is absolutely nothing in the list yet. So assign head and tail to the same tmp that you just created
c->tail = tmp;
c->head = tmp; // This needs to be done for your head2tail to work. Without this, your c-> head will be NULL always meaning nothing will print.
}
else { // This means that there is at least one Employee in the company. So don't touch the head. Add the new employee to the tail and then just modify the c->tail to point to the last employee that you just added.
c->tail->next = tmp;
c->tail = tmp;
}
}
這應該可以解決所有問題。當我運行整個代碼時,(在向 file1.txt 添加更多行之后)我看到:
1 sara laban [email protected] business 20 5 20 10 3000
2 Rami lawen [email protected] marketing 50 5 50 10 5000
3 Rami lawen [email protected] marketing 50 5 50 10 5000
4 sara laban [email protected] business 20 5 20 10 3000
5 Rami lawen [email protected] marketing 50 5 50 10 5000
6 Rami lawen [email protected] marketing 50 5 50 10 5000
7 sara laban [email protected] business 20 5 20 10 3000
8 Rami lawen [email protected] marketing 50 5 50 10 5000
9 Rami lawen [email protected] marketing 50 5 50 10 5000
10 sara laban [email protected] business 20 5 20 10 3000
11 Rami lawen [email protected] marketing 50 5 50 10 5000
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/385099.html
上一篇:將打開檔案的路徑傳遞給批處理檔案
下一篇:要求用戶輸入我稍后閱讀的檔案名
