我有这个结构:
using BETHEL_SPEAKER_SERVICE_TALK_INFO_S = struct tagBethelSpeakerServiceTalkInfo
{
CString strHost;
CString strCohost;
CString strChairman;
CString strOpenPrayer;
CString strSpeaker;
CString strTheme;
int iSongOpen{};
int iSongClose{};
};
它是类中的成员变量:
BETHEL_SPEAKER_SERVICE_TALK_INFO_S GetBethelServiceTalkInfo() const { return m_BSSTI; }
我想将此结构序列化到我的CArchive
. 目前我有存储:
ar << m_bSpecialEventBethelServiceTalk;
ar << m_BSSTI.strHost;
ar << m_BSSTI.strCohost;
ar << m_BSSTI.strChairman;
ar << m_BSSTI.strOpenPrayer;
ar << m_BSSTI.strSpeaker;
ar << m_BSSTI.strTheme;
ar << gsl::narrow<WORD>(m_BSSTI.iSongOpen);
ar << gsl::narrow<WORD>(m_BSSTI.iSongClose);
类似的阅读:
ar >> m_bSpecialEventBethelServiceTalk;
ar >> m_BSSTI.strHost;
ar >> m_BSSTI.strCohost;
ar >> m_BSSTI.strChairman;
ar >> m_BSSTI.strOpenPrayer;
ar >> m_BSSTI.strSpeaker;
ar >> m_BSSTI.strTheme;
m_BSSTI.iSongOpen = readAndCast<int, WORD>(ar);
m_BSSTI.iSongClose = readAndCast<int, WORD>(ar);
是否可以向结构本身添加<<
和运算符?>>
我是尝试operator<<
链接答案中提到的新手,但我遇到了问题。例如,我试过:
friend CArchive operator<<(CArchive& rArchive, BETHEL_SPEAKER_SERVICE_TALK_INFO_S const& rsBSSTI)
{
return rArchive << rsBSSTI.strHost
<< rsBSSTI.strCohost
<< rsBSSTI.strChairman
<< rsBSSTI.strOpenPrayer
<< rsBSSTI.strSpeaker
<< rsBSSTI.strTheme
<< gsl::narrow<WORD>(rsBSSTI.iSongOpen)
<< gsl::narrow<WORD>(rsBSSTI.iSongClose);
在我的代码的另一部分:
ar << m_BSSTI;
但是当我编译时:
7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(44,49): error C2061: syntax error:
标识符 'BETHEL_SPEAKER_SERVICE_TALK_INFO_S' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(45,2): error C2805: binary 'operator <<' 参数太少 7>D:\My Programs\ 2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(46,3):错误 C2059:语法错误:'return' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(53,48) : 错误 C2238: ';' 前面的意外标记 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(55,1): error C2143: syntax error: missing ';' 在 '}' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(55,1) 之前:错误 C2059:语法错误:'}' 7>AssignSelectedColumnDlg.cpp
到目前为止,这是完整的:
using BETHEL_SPEAKER_SERVICE_TALK_INFO_S = struct tagBethelSpeakerServiceTalkInfo
{
CString strHost;
CString strCohost;
CString strChairman;
CString strOpenPrayer;
CString strSpeaker;
CString strTheme;
int iSongOpen{};
int iSongClose{};
friend CArchive& operator<<(CArchive& rArchive, BETHEL_SPEAKER_SERVICE_TALK_INFO_S const& rsBSSTI)
{
return rArchive << rsBSSTI.strHost << rsBSSTI.strCohost
<< rsBSSTI.strChairman
<< rsBSSTI.strOpenPrayer
<< rsBSSTI.strSpeaker
<< rsBSSTI.strTheme
<< gsl::narrow<WORD>(rsBSSTI.iSongOpen)
<< gsl::narrow<WORD>(rsBSSTI.iSongClose);
}
};
上面的第一个错误在第 44 行,即:
friend CArchive& operator<<(CArchive& rArchive, BETHEL_SPEAKER_SERVICE_TALK_INFO_S const& rsBSSTI)