我正在创建一个 C++ 控制台应用程序,我在其中保存一个 vector 并将其加载到一个文件中。我正在保存和加载的文件有一个 header ,其大小与 vector 相同。
这是我的代码:
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
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());
}
这是头结构:
struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentsCount;
}fileHeader_t;
我收到以下错误:
E2379 Statement missing ; E2451 Undefined symbol 'fHeader'
在以下几行:
fileHeader_t fHeader;
为什么会发生这种情况,更重要的是,我该如何解决?
谢谢
更新
这是我的完整代码:
class appointment
{
public:
appointment(string aDate, string aTime, string aType,
string aLocation, string aComments, bool aIsImportant,
string aReminderDate, string aReminderTime)
{
appDate = aDate;
appTime = aTime;
appType = aType;
appLocation = aLocation;
appComments = aComments;
appIsImportant = aIsImportant;
appReminderDate = aReminderDate;
appReminderTime = aReminderTime;
}
void setDate(string aDate)
{
appDate = aDate;
}
void setTime(string aTime)
{
appTime = aTime;
}
void setType(string aType)
{
appType = aType;
}
void setLocation(string aLocation)
{
appLocation = aLocation;
}
void setComments(string aComments)
{
appComments = aComments;
}
void setIsImportant(bool aIsImportant)
{
appIsImportant = aIsImportant;
}
void setReminderDate(string aReminderDate)
{
appReminderDate = aReminderDate;
}
void setReminderTime(string aReminderTime)
{
appReminderTime = aReminderTime;
}
string getDate()
{
return appDate;
}
string getTime()
{
return appTime;
}
string getType()
{
return appType;
}
string getLocation()
{
return appLocation;
}
string getComments()
{
return appComments;
}
bool getIsImportant()
{
return appIsImportant;
}
string getReminderDate()
{
return appReminderDate;
}
string getReminderTime()
{
return appReminderTime;
}
private:
appointment();
string appDate;
string appTime;
string appType;
string appLocation;
string appComments;
bool appIsImportant;
string appReminderDate;
string appReminderTime;
//person owner;
};
class calendar
{
public:
calendar()
{
loadFromFile();
}
~calendar()
{
saveToFile();
}
void createAppointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime)
{
appointment newAppointment(aDate, aTime, aType, aLocation, aComments, aIsImportant, aReminderDate, aReminderTime);
appointments.push_back(newAppointment);
}
private:
vector<appointment> appointments;
string calCurrentDate;
string calCurrentTime;
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
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());
}
typedef struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentCount;
}fileHeader_t;
};
我收到以下 2 个错误:
[BCC32 警告] Person.cpp(271): W8060 可能分配不正确 完整的解析器上下文 Person.cpp(245): 类历 Person.cpp(290):决定实例化:void calendar::loadFromFile() --- 为实例化重置解析器上下文... Person.cpp(267): 解析:void calendar::loadFromFile()
[BCC32 错误] vector(608): E2247 'appointment::appointment()' 不可访问 完整的解析器上下文 vector(607): 决定实例化:void vector >::resize(unsigned int) --- 为实例化重置解析器上下文... Person.cpp(18): #include c:\program files (x86)\embarcadero\rad studio\9.0\include\dinkumware\vector vector (8):命名空间标准 vector (330):类 vector <_Ty,_Ax> vector(607): 解析:void vector >::resize(unsigned int)
我可以帮忙解决这个问题吗?
最佳答案
您在结构定义中缺少 typedef 关键字。
typedef struct fileHeader_s
{
}fileHeader_t;
但是,在 C++ 中,typedef 关键字不是必需的。在这种情况下,结构名称仍然是 fileHeader_s
。
关于C++ undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380738/