我正在使用 C++ Builder,并且我有一个Appointment
对象向量数组。
我想将其保存到文件中并从文件中加载。
目前,我将 ifstream 和 ofstream 与二进制文件一起使用。我有一个标题,其中包含将与数据一起保存的向量的大小,以便在加载时知道它的大小。
序列化是更好的方法吗?
如果是这样,我需要使用 boost 库还是其他方式?
这是我当前的代码:
class appointment
{
public:
appointment();
appointment(TDateTime aDate, TDateTime aReminderDateTime, string aType,
string aLocation, string aComments, bool aIsImportant)
{
appDateTime = aDate;
appReminderDateTime = aReminderDateTime;
appType = aType;
appLocation = aLocation;
appComments = aComments;
appIsImportant = aIsImportant;
}
void setAppDateTime(TDateTime aDateTime)
{
appDateTime = aDateTime;
}
void setappReminderDateTime(TDateTime aReminderDateTime)
{
appReminderDateTime = aReminderDateTime;
}
/*
void printAppointmentDetails()
{
cout << "Appointment Date: " << appDateTime << endl;
cout << "Appointment Reminder Date: " << appReminderDateTime << endl;
cout << "Appointment Type: " << appType << endl;
cout << "Appointment Location: " << appLocation << endl;
cout << "Appointment Comments: " << appComments << endl;
if (appIsImportant)
{
cout << "Appointment IsImportant: " << "Yes" << endl;
} else {
cout << "Appointment IsImportant: " << "No" << endl;
}
}
*/
void setType(string aType)
{
appType = aType;
}
void setLocation(string aLocation)
{
appLocation = aLocation;
}
void setComments(string aComments)
{
appComments = aComments;
}
void setIsImportant(bool aIsImportant)
{
appIsImportant = aIsImportant;
}
TDateTime getAppDateTime()
{
return appDateTime;
}
TDateTime getAppReminderDateTime()
{
return appReminderDateTime;
}
string getType()
{
return appType;
}
string getLocation()
{
return appLocation;
}
string getComments()
{
return appComments;
}
bool getIsImportant()
{
return appIsImportant;
}
private:
//appointment();
TDateTime appDateTime;
TDateTime appReminderDateTime;
string appType;
string appLocation;
string appComments;
bool appIsImportant;
//person owner;
};
class calendar
{
public:
calendar()
{
//loadFromFile();
//load persons
//calculateimportantAppointments
}
~calendar()
{
saveToFile();
}
//addperson
//editperson
//removeperson
void createAppointment(TDateTime aDate, TDateTime aReminderDateTime, string aType,
string aLocation, string aComments, bool aIsImportant)
{
appointment newAppointment(aDate, aReminderDateTime, aType,
aLocation, aComments, aIsImportant);
appointments.push_back(newAppointment);
}
/*
void printAllAppointmentDetails()
{
for (int i = 0; i < appointments.size(); i++)
{
appointments[i].printAppointmentDetails();
}
}
void calculateImportantAppointments()
{
}
int getNumberOfImportantAppointments()
{
int intImportantAppointmentCount = 0;
for (int i = 0; i < appointments.size(); i++)
{
if (appointments[i].getIsImportant())
intImportantAppointmentCount += 1;
}
return intImportantAppointmentCount;
}
appointment[] getImportantAppointments()
{
}
appointment[] getAllAppointments()
{
}
*/
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
if (!iStream)
{
cout << "No file";
} else {
fileHeader_t fHeader;
iStream.read((char*)&fHeader, sizeof(fileHeader_t));
if (fHeader.magicNumber = 0xDEADBEAF)
{
appointments.resize(fHeader.appointmentCount);
iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
}
}
}
void saveToFile()
{
ofstream oStream("file.ext", ios::binary);
fileHeader_t fHeader;
fHeader.magicNumber = 0xDEADBEAF;
fHeader.appointmentCount = appointments.size();
oStream.write((char*)&fHeader, sizeof(fileHeader_t));
oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
}
//vector<appointment> appointments;
private:
vector<appointment> appointments;
string calCurrentDate;
string calCurrentTime;
typedef struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentCount;
}fileHeader_t;
};
调用 loadFromFile() 方法时出现以下错误。
[BCC32 警告] File1.cpp(185): W8060 可能不正确的分配 [ILINK32 错误] 错误:从 \PROFILES.SOIT.LOCAL\HOMES$\SIMON.CANNING\MY DOCUMENTS\ 引用的未解决的外部 'appointment::appointment()' RAD STUDIO\PROJECTS\DEBUG\FILE1.OBJ [ILINK32 错误] 错误:无法执行链接
我知道这是由于构造函数调用而发生的。我可以就如何解决此问题提供一些建议吗?