Solved

Newbie problem with SerializeElements

Posted on 2002-05-20
3
463 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 106
Macro to save highlighted rows as a separate file 5 38
bigDiff challenge 17 75
EvenOdd challenge 10 83
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

20 Experts available now in Live!

Get 1:1 Help Now