问一个幼稚的问题,马上结财

yndfcd 2003-03-08 02:01:19
设有一个自己定义的类CMyClass,其关文件为myclass.h,实现代码在myclass.cpp中,现在在CMyDocument中要声明一个CMyClass类型的变量,大家一般怎么做?
是不是要在mydocument中包含myclass.h头文件?如果不想包含myclass.h还有别的方法吗?

在AppWizard生成的代码中,一般的头文件中没有任何#include 语句。但在View类的声明中却可能使用CXXDocument*的类型,编译器从来不报错,为什么?(例如上例中CMyView中肯定有一个这样的声明CMyDocument* GetDocument();但在CMyView的头文件中是绝对找不到CMyDocument的声明的,但编译仍能通过,如果在其中加入一个这样的声明CMyClass* GetClass();编译肯定通不过)
...全文
36 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
root60931 2003-03-10
  • 打赏
  • 举报
回复
友情混分!
winphoenix 2003-03-10
  • 打赏
  • 举报
回复
为什么不解贴
szhelp 2003-03-10
  • 打赏
  • 举报
回复
CMyClass是用什么方法实例化的,假如是通过定义类变量如在CSdfsdfsdfsdfsView类声明中
定义,那么在CSdfsdfsdfsdfsView类的头文件中#include.
如果是在CSdfsdfsdfsdfsView类的成员函数中动态创建如在
int CSdfsdfsdfsdfsView::OnCreate(...)中创建,CMyClass* pMyClass = new CMyClass;
那么在只要在cpp文件中#include
demetry 2003-03-09
  • 打赏
  • 举报
回复
楼主在仔细看一看

&_&
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
那么在MSDN中的例子是怎么回事?在enroldoc.h中,它即没有#include "sectset.h"也没有使用class CSectionSet 这样的语句,只在enroldoc.cpp中有一个#include "sectset.h",但仍然能通过编译,是不是有什么编译选项,可以用来控制?
posy 2003-03-08
  • 打赏
  • 举报
回复
NOldkiller(学习学习再学习!) 说的是对的。
但在VC6下声明指针也决不会有问题。CMyDocument* CMyView::GetDocument()就是一个例子。

否则,如果需要达到这样的目的该怎么办?

class CA
{
CB* m_pB; //Note: it's a pointer, not an instance - "CB m_b";
};
class CB
{
CA* m_pA; //Note: it's a pointer, not an instance - "CA m_a";
};

这里可以规定几个术语:

这叫做类的声明:
class CA;

这叫做类的定义:
class CA
{
//......
};

实现类的函数体叫做类的实现。

声明类的实例变量时需要类的定义,而声明指针变量时仅需要类的声明,
NOldkiller 2003-03-08
  • 打赏
  • 举报
回复
不好意思,再补充一下,我在机器上试了一下VC6(装了SP5),支持前向声明。


class CMyClass;

class CSdfsdfsdfsdfsView : public CView
{
protected: // create from serialization only
CSdfsdfsdfsdfsView();
DECLARE_DYNCREATE(CSdfsdfsdfsdfsView)

// Attributes
public:
CSdfsdfsdfsdfsDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSdfsdfsdfsdfsView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CSdfsdfsdfsdfsView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:
CMyClass* m_MyClass;
// Generated message map functions
protected:
//{{AFX_MSG(CSdfsdfsdfsdfsView)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG // debug version in sdfsdfsdfsdfsView.cpp
inline CSdfsdfsdfsdfsDoc* CSdfsdfsdfsdfsView::GetDocument()
{ return (CSdfsdfsdfsdfsDoc*)m_pDocument; }
#endif
NOldkiller 2003-03-08
  • 打赏
  • 举报
回复
对,这就是VC的原因了,不支持前向声明。
候捷在<Disectinng MFC>中也是同感。
直接#include 吧。
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
出现错误,use undefined class CMyClass。
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
NOldkiller(学习学习再学习!)
你说的方法在VC++5.0上可以,但是在6.0上就不行了。
NOldkiller 2003-03-08
  • 打赏
  • 举报
回复
当然不可以,因为编译器根本不知道CMyClass是什么,
再加上一句前向声明
class CMyclass;
jack_wq 2003-03-08
  • 打赏
  • 举报
回复
#include就可以了!
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
to NOldkiller(学习学习再学习!) :

似乎不是你所说的原因,因为我在CMyDocument中声明CMyClass* m_pMyClass,编译仍然不能通过。编译器仍然认为CMyClass为未定义的变量类型。
NOldkiller 2003-03-08
  • 打赏
  • 举报
回复
AppWizard生成的代码中,一般的头文件中没有任何#include 语句。但在View类的声明中却可能使用CXXDocument*的类型,编译器从来不报错,为什么?

因为这是一个指针类型,而指针无论是什么类型的指针,其大小肯定是已知的,比如是32位机器上就是4个字节。但如果是一个非指针自定义类型,如果编译器没有看到其声明,则其大小对编译器是未知的,编译当然不能通过。
直接将对应自定义的类型包括在用到此类型的头文件中吧。如果放在Stdafx.h,此自定义类型头文件的任何改变都会导致预编译头文件的重新编译,这样就失去了预编译的意义了。
fupf88 2003-03-08
  • 打赏
  • 举报
回复
he和编译次序有关,stdafx要预先编译的,放在mydocoment.cpp编译器还不知道有myclass这个类。
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
难道这个问题很难吗?
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
请看一个MSDN,例程\vc98\mfc\database\dynabind;这个例子中的CEnrollDoc中定义一个变量CSectionSet m_sectionSet;在头文件enroldoc.h中也看不到一个#include 语句,只在enroldoc.cpp中有一个#include "sectset.h"(CSectionSet的声明在此文件中)这个例子也能通过编译。为什么?
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
现在我己经知道了只要将#include "myclass.h"放到stdafx.h中就行了。但还是不知道为会什么放在mydocoment.cpp中就不行。
88dd 2003-03-08
  • 打赏
  • 举报
回复
myclass.h开头加 #pragma once
在stdafx.h中加 #include "myclass.h"
yndfcd 2003-03-08
  • 打赏
  • 举报
回复
没办法最多只能加到100分。
加载更多回复(3)

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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