0

I have this structure:

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{};
};

It is a member variable in a class:

BETHEL_SPEAKER_SERVICE_TALK_INFO_S GetBethelServiceTalkInfo() const { return m_BSSTI; }

I want to serialize this structure to my CArchive. At the moment I have for storing:

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);

Similar for reading:

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);

Is it possible to add << and >> operators to the structure itself?


I am new to trying out the operator<< referred to in the linked answer but I am having issues. For example, I tried:

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);

And in the other part of my code:

ar << m_BSSTI;

But when I compile:

7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(44,49): error C2061: syntax error:

identifier 'BETHEL_SPEAKER_SERVICE_TALK_INFO_S' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(45,2): error C2805: binary 'operator <<' has too few parameters 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(46,3): error C2059: syntax error: 'return' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(53,48): error C2238: unexpected token(s) preceding ';' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(55,1): error C2143: syntax error: missing ';' before '}' 7>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\SpecialEventManager.h(55,1): error C2059: syntax error: '}' 7>AssignSelectedColumnDlg.cpp


Here is the full thing so far:

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);
    }
};

With the above the first error is on line #44 which is:

friend CArchive& operator<<(CArchive& rArchive, BETHEL_SPEAKER_SERVICE_TALK_INFO_S const& rsBSSTI)
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • @RaymondChen Let me try and digest the liked answer before I accept it as the resolution. But it sounds promising. – Andrew Truckle Feb 10 '22 at 15:12
  • @RaymondChen I am having problems trying to implement the linked answer. – Andrew Truckle Feb 10 '22 at 15:31
  • 1
    There's not enough information in your question to be of much assistance. Which line is line 45? Where did you put the `friend` declaration relative to the structure definition? What is "the other part of my code"? – Raymond Chen Feb 10 '22 at 15:39
  • @RaymondChen See updated question. – Andrew Truckle Feb 10 '22 at 15:40
  • 1
    Notice that the error message is "syntax error: dentifier 'BETHEL_SPEAKER_SERVICE_TALK_INFO_S'". You are using `BETHEL_SPEAKER_SERVICE_TALK_INFO_S` inside the definition of `tagBethelSpeakerServiceTalkInfo` before `BETHEL_SPEAKER_SERVICE_TALK_INFO_S` has been defined. (It doesn't become defined until after the `using` statement is complete.) Has nothing to do with overloading. Inside the `tagBethelSpeakerServiceTalkInfo` definition, you have to call the class `tagBethelSpeakerServiceTalkInfo`. The alias name is not available until later. (But why create an alias anyway?) – Raymond Chen Feb 10 '22 at 15:42
  • @RaymondChen That was it. Historically I just used the tags. Partly because I preferred the ClassView showing my list of tag names which makes much more sense. – Andrew Truckle Feb 10 '22 at 15:44
  • This question also helped: https://stackoverflow.com/questions/22646705/overloading-with-carchive-operator – Andrew Truckle Feb 10 '22 at 15:46

0 Answers0