Solved

Newbie problem with SerializeElements

Posted on 2002-05-20
3
467 Views
Last Modified: 2013-11-20
Hi

I'm getting the following error while implementing SerializeElements:

"'CArchive::CArchive': cannot access protected member declared in class 'CArchive'"

This is the code I am using:

template <> 
void AFXAPI SerializeElements <CStockData>
( CArchive& ar, CStockData* pNewSD, int nCount )
{    
     for ( int i = 0; i < nCount; i++, pNewSD++ )
          pNewSD->Serialize( ar );
}

and it is defined in the header as:

template <> 
void AFXAPI SerializeElements <CStockData>
( CArchive& ar, CStockData* pNewSD, int nCount );

outside of the class declarations.

The SerializeElements code is sitting in a class (CStockDataList: public CList< CStockData, CStockData & > ) that adds CStockData objects (derived from CObject) to a list.  The StockData.h has been included in StockDataList.h as well as afxtempl.h for CList.


CStockData has been serialised by using
IMPLEMENT_SERIAL(CStockData, CObject, 1) in the .cpp and
DECLARE_SERIAL(CStockData) in the .h

I use the following code for CStockData Serialize  (All data members are protected):

void CStockData::Serialize(CArchive ar)
{
     if (ar.IsStoring ())
          ar << m_date << m_dblPrice << m_strFund;
     else
          ar >> m_date >> m_dblPrice >> m_strFund;
}



I'm still pretty new to MFC so I'm not really sure what the error message is trying to tell me, or if I'm even looking in the right place for the solution.  I've looked in the MSDN and the code I'm using for SerializeElements seems to be correct, so I'm stumped as to what I'm missing.

Any help would be greatly appreciated.

Thanks in advance.
0
Comment
Question by:boomshanker
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
kender_a earned 100 total points
ID: 7156510
Hi!

It looks like your function is geared for MFC CObjects & CArchives.

You don't need to write a template function for that (unless you have some special non-MFC reasons for making a template functions). Since method Serialize(...) is defined as virtual in the CObject, it's more conventional to declare your function like this:

 void AFXAPI SerializeElements
 ( CArchive& ar, CObject* pNewSD, int nCount );

Implementation remains the same as yours.

You are getting the error regarding protected CArchive::CArchive because somewhere in your code you have constructed a CArchive without constructor arguments i.e.:

 CArchive ar;

The problem is caused by CStockData::Serialize(CArchive ar)
- it receives CArchive by value, but it shoud receive it by reference.
Should be CStockData::Serialize(CArchive& ar).

Sincerely,
Kender

 
0
 
LVL 1

Author Comment

by:boomshanker
ID: 7160490
Thanks!!!

I had actually given up on this project (and almost on MFC!!!).

You were right on the money.  I was using CArchive ar instead of &ar.  As soon as I changed that, it worked.

Thanks again.
0
 
LVL 1

Expert Comment

by:kender_a
ID: 7161479
You're wellcome at any time!

Kender
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 92
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
sum28 challenge 31 105
Excel file not created as expected 7 46
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now