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
Solved

Newbie problem with SerializeElements

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 218
Complete beginner needs help making a cron job 9 125
viewing source code from eclipse 13 110
lucky13 challenge 11 154
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…
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.

856 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