互相包含---自定义类的成员函数需调用视图类的成员函数时如何写include

wwwlgb 2000-06-30 02:54:00

用MFC的应用向导生成的应用程序,如果视图类,比如CTestSocketServerView含有一个类型为用类向导生成的类,比如CListenSocket,的成员。那这个自定义的类CListenSocket的成员函数,如想调用视图类CTestSocketServerView的成员函数,在CListenSocket的实现文件ListenSocket.cpp中仅仅包含CTestSocketServerView的头文件TestSocketServerView.h编译不能通过,必须同时包含相应框架类的头文件MainFrm.h、文档类的头文件TestSocketServerDoc.h"、视图类的头文件TestSocketServerView.h、ListenSocket.h编译才能通过,可我的自定义的类CListenSocket并没有调用框架类或文档类的东东!不知何故,请各位指点。

CListenSocket含有一个成员m_pView,它是CTestSocketServerView类型的指针
CTestSocketServerView派生自CFormView,含有一个文本框txtCount,它还有一个l个类型为CListenSocket的 m_listen成员。
“***“是我加的说明。

CListenSocket的头文件:ListenSocket.h
#if !defined(AFX_LISTENSOCKET_H__9C066807_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_)
#define AFX_LISTENSOCKET_H__9C066807_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_

//#include "TestSocketServerView.h" // Added by ClassView //***注释掉
class CTestSocketServerView; //***前向声明
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ListenSocket.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CListenSocket command target

class CListenSocket : public CSocket
{
// Attributes
public:

// Operations
public:
CListenSocket();
virtual ~CListenSocket();

// Overrides
public:
CTestSocketServerView *m_pView;
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CListenSocket)
public:
virtual void OnAccept(int nErrorCode);
//}}AFX_VIRTUAL

// Generated message map functions
//{{AFX_MSG(CListenSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

// Implementation
protected:
private:
CArray<long,long&> m_aService; //*** stdafx.h中已经
//***包含afxtempl.h
};

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

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_LISTENSOCKET_H__9C066807_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_)

ListenSocket.cpp :CListenSocket的实现文件
// ListenSocket.cpp : implementation file
//

#include "stdafx.h"
#include "ServiceSocket.h"
#include "TestSocketServer.h"
#include "MainFrm.h" //***必须同时包含框架、文档
#include "TestSocketServerDoc.h" //***视图类的头文件,仅仅包含
#include "TestSocketServerView.h" //***视图类的头文件则编译出错!怪!!!
#include "ListenSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListenSocket

CListenSocket::CListenSocket()
{
}
CListenSocket::~CListenSocket()
{
int nUp=this->m_aService.GetUpperBound();
CServiceSocket *pskt;
for(int i=0;i<nUp+1;i++){
pskt=(CServiceSocket *)(m_aService[i]);
delete pskt;
}
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CSocket)
//{{AFX_MSG_MAP(CListenSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CListenSocket member functions

void CListenSocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class

if(nErrorCode) return;
static nCount=0;
nCount++;
CServiceSocket skt;
this->Accept(skt);
CString str;
str.Format("Request=%d",nCount);
//***
m_pView->m_txtCount.SetWindowText(str); //***此处调用视图类
//的成员函数
long addr= (long)((void *)&skt);
m_aService.Add(addr);
CFile f;
f.Open("Counter.txt",CFile::modeCreate);
f.SeekToBegin();
f.Write((void *)&nCount,sizeof(int));
f.Close();
//CSocket::OnAccept(nErrorCode);
}


视图类CTestSocketServerView的头文件:TestSocketServerView.h
// TestSocketServerView.h : interface of the CTestSocketServerView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_TESTSOCKETSERVERVIEW_H__9C0667FF_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_)
#define AFX_TESTSOCKETSERVERVIEW_H__9C0667FF_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_

#include "ListenSocket.h" //Added by ClassView
//class CListenSocket; //*** 此处不能用前向声明代替,
//必须用#include "ListenSocket.h"???

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CTestSocketServerView : public CFormView
{
protected: // create from serialization only
CTestSocketServerView();
DECLARE_DYNCREATE(CTestSocketServerView)

public:
//{{AFX_DATA(CTestSocketServerView)
enum { IDD = IDD_TESTSOCKETSERVER_FORM };
CEdit m_txtCount;
//}}AFX_DATA

// Attributes
public:
CTestSocketServerDoc* GetDocument();

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestSocketServerView)
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void OnInitialUpdate(); // called first time after construct
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL

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

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CTestSocketServerView)
afx_msg void OnStart();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CListenSocket m_listen; //***类型为自定义类CListenSocket的成员
};

#ifndef _DEBUG // debug version in TestSocketServerView.cpp
inline CTestSocketServerDoc* CTestSocketServerView::GetDocument()
{ return (CTestSocketServerDoc*)m_pDocument; }
#endif

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

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TESTSOCKETSERVERVIEW_H__9C0667FF_4BDA_11D4_8FAD_0050BA0694EB__INCLUDED_)


视图类CTestSocketServerView的实现文件:TestSocketServerView.cpp
// TestSocketServerView.cpp : implementation of the CTestSocketServerView class
//

#include "stdafx.h"
#include "TestSocketServer.h"

#include "TestSocketServerDoc.h"
#include "TestSocketServerView.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestSocketServerView

IMPLEMENT_DYNCREATE(CTestSocketServerView, CFormView)

BEGIN_MESSAGE_MAP(CTestSocketServerView, CFormView)
//{{AFX_MSG_MAP(CTestSocketServerView)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestSocketServerView construction/destruction

CTestSocketServerView::CTestSocketServerView()
: CFormView(CTestSocketServerView::IDD)
{
//{{AFX_DATA_INIT(CTestSocketServerView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
//m_listen.m_pView=this;
}

CTestSocketServerView::~CTestSocketServerView()
{
m_listen.m_pView=NULL;
}

void CTestSocketServerView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestSocketServerView)
DDX_Control(pDX, IDC_EDIT1, m_txtCount);
//}}AFX_DATA_MAP
}

BOOL CTestSocketServerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CFormView::PreCreateWindow(cs);
}

void CTestSocketServerView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CTestSocketServerView printing

BOOL CTestSocketServerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CTestSocketServerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CTestSocketServerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

void CTestSocketServerView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CTestSocketServerView diagnostics

#ifdef _DEBUG
void CTestSocketServerView::AssertValid() const
{
CFormView::AssertValid();
}

void CTestSocketServerView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}

CTestSocketServerDoc* CTestSocketServerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestSocketServerDoc)));
return (CTestSocketServerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestSocketServerView message handlers

void CTestSocketServerView::OnStart()
{
// TODO: Add your control notification handler code here
m_listen.m_pView=this;
m_listen.Create(2500);
m_listen.Listen();
}

请各位高手指教!
...全文
162 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwlgb 2000-07-05
  • 打赏
  • 举报
回复
谢谢ZPoint了!你的解答让我明白了一些。如能告诉我“...”后的内容就更好了。我则几天一直在查资料,想知道“...”后的内容,还是没弄清楚。
wwwlgb 2000-06-30
  • 打赏
  • 举报
回复
"所以编译过程中..."后面是什么?

ZPoint 2000-06-30
  • 打赏
  • 举报
回复
首先,你不必把这一大堆的东西贴上来。
在include的方法中,入门者或者经验不够的程序员常常有这种疑问。
在编译时,VC会先编译头文件,在编译CListenSocket时由于发生了include"TestSocketServerView.h",自然会对TestSocketServerView.h进行编译,再者,TestSocketServerView.h中申明了CTestSocketServerDoc和CMainFrame,所以编译过程中...

16,472

社区成员

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

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

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