Solved

Newbie problem with SerializeElements

Posted on 2002-05-20
3
475 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
format the code in java 6 94
canBalance challenge 34 85
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 128
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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