Solved

Newbie problem with SerializeElements

Posted on 2002-05-20
3
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

634 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