求救:一个关于从CObject派生新类的问题!

piano9 2001-12-16 01:30:32
为了使我的程序可以存档,我做了个派生自CObject的类CCoco(包含一个CRect类和一个bool型变量),但是编译时出现以下错误:
Compiling...
StdAfx.cpp
e:\practise\rec\coco.h(23) : error C2146: syntax error : missing ';' before identifier 'm_rect'
e:\practise\rec\coco.h(23) : error C2501: 'CRect' : missing storage-class or type specifiers
e:\practise\rec\coco.h(23) : error C2501: 'm_rect' : missing storage-class or type specifiers
e:\practise\rec\coco.h(33) : error C2629: unexpected 'class CCoco ('
e:\practise\rec\coco.h(33) : error C2238: unexpected token(s) preceding ';'
Error executing cl.exe.

以下为源程序:
coco.h
/////////////////////////////////////////////////////////////////////////////



class CCoco : public CObject
{
protected: // create from serialization only
CCoco();


// Attributes
public:

bool m_bsharp;
CRect m_rect;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCoco)
public:
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL

// Implementation
public:
CCoco(CRect rect,int i);
DECLARE_SERIAL(CCoco)

void setrect(){ m_bsharp=TRUE;}
void setellipse(){m_bsharp=FALSE;}

virtual ~CCoco();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

};

/////////////////////////////////////////////////////////////////////////////


实现文件如下:
coco.cpp
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "rec.h"

#include "coco.h"


/////////////////////////////////////////////////////////////////////////////
// CCoco

IMPLEMENT_SERIAL(CCoco, CObject,1)


/////////////////////////////////////////////////////////////////////////////
// CCoco construction/destruction

CCoco::CCoco()
{
m_bsharp=TRUE;
}

CCoco::~CCoco()
{
}




/////////////////////////////////////////////////////////////////////////////
// CCoco serialization

void CCoco::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar<<m_rect;
ar<<m_bsharp;
}
else
{
ar>>m_rect;
ar>>m_bsharp;
}
}

/////////////////////////////////////////////////////////////////////////////
// CCoco diagnostics

#ifdef _DEBUG
void CCoco::AssertValid() const
{
CDocument::AssertValid();
}

void CCoco::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCoco commands


CCoco::CCoco(CRect rect, int i)
{
m_rect.CopyRect(&rect);
if(i==1)
m_bsharp=TRUE;
else if(i==2)
m_bsharp=FALSE;
}
...全文
98 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hujun614 2001-12-16
  • 打赏
  • 举报
回复
我觉得,可以肯定的是,
e:\practise\rec\coco.h(23) : error C2146: syntax error : missing ';' before identifier 'm_rect'
e:\practise\rec\coco.h(23) : error C2501: 'CRect' : missing storage-class or type specifiers
e:\practise\rec\coco.h(23) : error C2501: 'm_rect' : missing storage-class or type specifiers
e:\practise\rec\coco.h(33) : error C2629: unexpected 'class CCoco ('
e:\practise\rec\coco.h(33) : error C2238: unexpected token(s) preceding ';'
Error executing cl.exe.

的原因是CRect没有说明,
可以加入在前面加个class CRect;应该没有问题
lzwcom 2001-12-16
  • 打赏
  • 举报
回复
试试,因为不知道你创建工程的步骤,所以用下面的方法试试看。
class CRect;
class CCoco : public CObject
{
}
jsyou 2001-12-16
  • 打赏
  • 举报
回复
<afxwin.h> 头文件加在stdafx.h中。
piano9 2001-12-16
  • 打赏
  • 举报
回复
不行呀,大虾,在coco.h中包含afxwin.h后,错误如下:
Compiling...
coco.cpp
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
E:\Practise\rec\coco.cpp(43) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)
E:\Practise\rec\coco.cpp(54) : error C2352: 'CDocument::AssertValid' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(4518) : see declaration of 'AssertValid'
E:\Practise\rec\coco.cpp(59) : error C2352: 'CDocument::Dump' : illegal call of non-static member function
c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(4517) : see declaration of 'Dump'
rec.cpp
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
recDoc.cpp
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
recView.cpp
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
e:\practise\rec\coco.h(7) : error C2011: 'CCoco' : 'class' type redefinition
E:\Practise\rec\recView.cpp(22) : error C2065: 'pDC' : undeclared identifier
E:\Practise\rec\recView.cpp(22) : error C2227: left of '->SelectObject' must point to class/struct/union
E:\Practise\rec\recView.cpp(25) : error C2227: left of '->Rectangle' must point to class/struct/union
E:\Practise\rec\recView.cpp(28) : error C2227: left of '->Ellipse' must point to class/struct/union
E:\Practise\rec\recView.cpp(31) : error C2601: 'CreateObject' : local function definitions are illegal
E:\Practise\rec\recView.cpp(31) : error C2601: '_GetBaseClass' : local function definitions are illegal
E:\Practise\rec\recView.cpp(31) : error C2655: 'classCRecView' : definition or redeclaration illegal in current scope
e:\practise\rec\recview.h(17) : see declaration of 'classCRecView'
E:\Practise\rec\recView.cpp(31) : error C2601: 'GetRuntimeClass' : local function definitions are illegal
E:\Practise\rec\recView.cpp(33) : error C2601: '_GetBaseMessageMap' : local function definitions are illegal
E:\Practise\rec\recView.cpp(33) : error C2601: 'GetMessageMap' : local function definitions are illegal
E:\Practise\rec\recView.cpp(33) : error C2655: 'messageMap' : definition or redeclaration illegal in current scope
e:\practise\rec\recview.h(54) : see declaration of 'messageMap'
E:\Practise\rec\recView.cpp(33) : error C2655: '_messageEntries' : definition or redeclaration illegal in current scope
e:\practise\rec\recview.h(54) : see declaration of '_messageEntries'
E:\Practise\rec\recView.cpp(45) : error C2143: syntax error : missing ';' before '{'
E:\Practise\rec\recView.cpp(50) : error C2143: syntax error : missing ';' before '{'
E:\Practise\rec\recView.cpp(54) : error C2601: 'PreCreateWindow' : local function definitions are illegal
E:\Practise\rec\recView.cpp(65) : error C2601: 'OnDraw' : local function definitions are illegal
E:\Practise\rec\recView.cpp(82) : error C2601: 'AssertValid' : local function definitions are illegal
E:\Practise\rec\recView.cpp(87) : error C2601: 'Dump' : local function definitions are illegal
E:\Practise\rec\recView.cpp(92) : error C2601: 'GetDocument' : local function definitions are illegal
E:\Practise\rec\recView.cpp(102) : error C2601: 'OnLButtonDown' : local function definitions are illegal
E:\Practise\rec\recView.cpp(120) : error C2601: 'OnRect' : local function definitions are illegal
E:\Practise\rec\recView.cpp(125) : error C2601: 'OnEllipse' : local function definitions are illegal
E:\Practise\rec\recView.cpp(128) : fatal error C1004: unexpected end of file found
Generating Code...
Error executing cl.exe.

rec.exe - 32 error(s), 0 warning(s)
xiaoxiaohan 2001-12-16
  • 打赏
  • 举报
回复
在你的头文件中加入
#include <afxwin.h>
sunwen 2001-12-16
  • 打赏
  • 举报
回复
包含CRect所在的h文件!

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧