互相包含---自定义类的成员函数需调用视图类的成员函数时如何写include
用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();
}
请各位高手指教!