Click to See Complete Forum and Search --> : Serializing a CObject derived class


Ed Handrich
June 10th, 1999, 11:24 AM
I have followed all the rules in the online help for deriving a class from CObject. When a use the >> and << operators in my DOC class, I get a compiler error C2679 : binary '<<' : no operator defined which takes a right-hand operand of type 'class CBitmapFile' (or there is no acceptable conversion).

Here is my CBitmapFile header

class CBitmapFile : public CObject
{
DECLARE_SERIAL( CBitmapFile )

private:
BITMAPFILEHEADER bmfh;
BITMAPINFO bmi;
LPVOID ptrPixels;
CString fname;
int error;

public:
CBitmapFile();
~CBitmapFile();

virtual void Serialize( CArchive& ar );

int Width() { return (int)bmi.bmiHeader.biWidth; }
int Height() { return (int)bmi.bmiHeader.biHeight; }

int GetError( CString *pstrError = NULL );

void Empty();
void DrawBitmap( CDC *pDC, int sf );
};

and here is part of the CPP file

#include "stdafx.h"
#include "bmfile.h"

IMPLEMENT_SERIAL( CBitmapFile, CObject, 1 )

CBitmapFile::CBitmapFile()
{
ptrPixels = NULL; // make sure the pointer is NULL
Empty(); // make an empty bitmap
}

now why can't I do this in my Doc class

void CMyDoc::Serialize( CArchive& ar )
{
if( ar.IsLoading())
ar >> bmf;
else
ar << bmf;
}

June 10th, 1999, 01:02 PM
I'm still learning MFC myself, but this is what I'd try:

1) Define Serialize() in your CBitmapFile.cpp file, like this:
void CBitmapFile::Serialize( CArchive& ar )
{
if( ar.IsLoading())
ar >> bmfh >> bmi >> ptrPixels >> fname >> error;
else
ar << bmfh << bmi << ptrPixels << fname << error:
}

2. In your CMyDoc's Serialize function, you just call bmf's serialize routine:
void CMyDoc::Serialize( CArchive& ar )
{
bmf.Serialize(ar);
}


I assume you declared bmf as a CBitmapFile object of CMyDoc.
The computer I'm using now doesn't have Visual C++ loaded, so I can't try this myself; it's just a suggestion.
Good luck.

- Karen

Ed Handrich
June 10th, 1999, 02:54 PM
I found the answer myself in ONE sentence of ALL the help files relating to Serializing objects. It says you can't use << and >> operators on a CObject. This has got to be one of the MOST stupid things I have ever seen in MFC. If I have to call the serialize function, then
1) Why bother deriving from CObject ?
2) Why bother using the DECLARE_SERIAL macro ?
3) Why bother using the IMPLEMENT_SERIAL macro ?

I just left my class the way it is, and didn't derive from anything, took out the macros, and called the serialize function directly. It works fine. What a BIG waste of time it is to derive from CObject, but that's just my opinion.