我从CObject派生了一个新类CShape然后又从CShape派生了一个CLine我在CShape里用了
DECLARE_SERIAL(CShape)  和    IMPLEMENT_SERIAL(CShape,CObject,1)
编译正确;但是我在CLine里
DECLARE_SERIAL(CLine)
IMPLEMENT_SERIAL(CLine,CShape,1)
却编译出现了问题,好像runtime class 里得那些函数都找不到了,这样用有错误吗?高手告诉我,该怎么实现阿???

解决方案 »

  1.   

    我的例子:
    class CDrawDataTag:public CObject
    {
    public:
      CPoint point;//used for the line
      int radius;//used for the arch
      BOOL  m_bFlag;//true for drawing  line ,false  for drawing  arch!
      CDrawDataTag(CPoint pt,int rd);
    public:
      virtual void Serialize(CArchive &ar);protected:  
      //support serliaizing
      CDrawDataTag( );
      DECLARE_SERIAL(CDrawDataTag)
    };//the beginning of  implementation of class CDrawDataTagIMPLEMENT_SERIAL(CDrawDataTag,CObject,1)CDrawDataTag::CDrawDataTag()//used by CreateObject();
    {
      //only used by serializing!
    }
    CDrawDataTag::CDrawDataTag(CPoint pt ,int rd):point(pt),radius(rd)
    {
    this->m_bFlag=true;
    }
    void CDrawDataTag::Serialize(CArchive &ar)
    {
      //important!!!
      //序列化的功能很是强大!!
      //宋业文 2002年10月5日
      if(ar.IsStoring())
      {
     ar<<(WORD)point.x<<(WORD)point.y<<(WORD)radius<<(WORD)m_bFlag;
      }
      else
      {
      WORD x,y,rd,fg;
      ar>>x;
      ar>>y;
      ar>>rd;
      ar>>fg;
      point.x=x;
      point.y=y;
      radius=rd;
      m_bFlag=fg;
      }}
      

  2.   

    谢谢psusong,不过我想知道的是从CObject派生出来的CShpae类做父类派生出CLine怎么实现序列话?我在机子上编译的时候出现了问题。就是那些CRuntimeClass里得函数它都说找不到:(
    那个IMPLEMENT_SERIAL宏一展开就出现这么多的错误,天啊
    为什么用CObject直接做基类就可以????
    Line.cpp
    e:\my software\geometry\line.h(18) : error C2143: syntax error : missing ';' before 'public'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2509: 'CreateObject' : member function not declared in 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2509: '_GetBaseClass' : member function not declared in 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2039: 'classCLine' : is not a member of 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2248: '_GetBaseClass' : cannot access protected member declared in class 'CShape'
            e:\my software\geometry\shape.h(23) : see declaration of '_GetBaseClass'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2509: 'GetRuntimeClass' : member function not declared in 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2039: 'classCLine' : is not a member of 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    E:\MY SOFTWARE\Geometry\Line.cpp(19) : error C2039: 'classCLine' : is not a member of 'CLine'
            e:\my software\geometry\line.h(14) : see declaration of 'CLine'
    Error executing cl.exe.Line.obj - 8 error(s), 0 warning(s)
      

  3.   

    serialable要几个条件,其中一个是你的类必须要有缺省构造函数
                                                      ----侯sir说CLine::CLine()
    {
    }
      

  4.   

    应该是由文件的问题#include "shape.h"  // 可能是这里class CLine:public CShape
    {
    DECLARE_SERIAL(CLine)
    }IMPLEMENT_SERIAL(CLine,CObject,0)
      

  5.   

    检查一下你的CLine类的定义,特别是DECLARE_SERIAL(CLine)之前的一句。是不是缺了一个“;”。
      

  6.   

    // Shape.h: interface for the CShape class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_SHAPE_H__3907A100_31D9_4096_ADAA_BF06480738A6__INCLUDED_)
    #define AFX_SHAPE_H__3907A100_31D9_4096_ADAA_BF06480738A6__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000class CShape : public CObject  
    {
    public:
    virtual void Serialize(CArchive &ar);
    CShape();
    virtual ~CShape();
    DECLARE_SERIAL(CShape)////////////////////////////////
    public:
    virtual BOOL Draw(CDC *pDC);};#endif // !defined(AFX_SHAPE_H__3907A100_31D9_4096_ADAA_BF06480738A6__INCLUDED_)// Line.h: interface for the CLine class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_LINE_H__CD0074B9_D4CF_4D58_BF6E_1252CA47EFFF__INCLUDED_)
    #define AFX_LINE_H__CD0074B9_D4CF_4D58_BF6E_1252CA47EFFF__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#include "Shape.h"class CLine : public CShape  
    {
    protected:
    DECALRE_SERIAL(CLine)
    public:
    CLine();
    virtual ~CLine();
    virtual void Serialize(CArchive &ar);};#endif // !defined(AFX_LINE_H__CD0074B9_D4CF_4D58_BF6E_1252CA47EFFF__INCLUDED_)各位大哥,头文件就这个样子,我还没有开始往里边添函数和变量就这个样子了
    搞死掉了
      

  7.   

    大哥们,我错了:(
    原来是我把DECLARE写成了DECALRE我真的错了,跟贴的都给分吧,谢谢
      

  8.   

    再说几句,好象gboy(hello) 说的也没错,写这IMPLEMENT_SERIAL(CLine,CObject,0)也可以吧。