2012-09-12 168 views
0

我正在创建一个C++控制台应用程序,用于保存矢量并将其加载到文件中。我正在保存和加载的文件有一个标题,它具有矢量的大小。C++未定义符号

这里是我的代码:

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):class calendar Person.cpp(290):决定实例化:void calendar :: loadFromFile() ---重置实例化解析器上下文... Person.cpp(267):解析:void calendar: :loadFromFile()

[BCC32错误]载体(608):E2247 '约会预约::()' 不可访问 全解析器上下文 矢量(607):决定实例:无效矢量> ::调整大小( (18):#include c:\ program files(x86)\ embarcadero \ rad studio \ 9.0 \ include \ dinkumware \ vector vector(8)unsigned int) ---重置解析器上下文以实例化... ):namespace std vector(330):class vector < _Ty,_Ax> vector(607):parsing:void vector> :: resize(unsigned int)

我可否请帮助解决此问题?

+0

你忘了'struct'。 –

+0

谢谢。我更新了我的问题。我可以请一些帮助吗? –

+0

@DarrylJanecek:提示:问题是'private:appointment();'。 –

回答

2

您在结构定义中缺少typedef关键字。

typedef struct fileHeader_s 
{ 

}fileHeader_t; 

但是,在C++中,typedef关键字不是必需的。在那种情况下,struct name仍然是fileHeader_s

0

我想你想要的是一个typedef

typedef struct fileHeader_s 
{ 
DWORD magicNumber; 
size_t appointmentsCount; 
}fileHeader_t;