socket声明问题 今天照做个例子,从CSocket派生了个类?我声明成一个变量,怎么也通不过?后来换了下声明成一个指针变量就通过了?不知道为什么?想问下什么情况下变量一定要声明成指针?什么时候指针一般类型皆可??谢谢回复!!!!!!!!!!!!感激......... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我能想到的有如下两种情况:(1)你在此类的声明中,定了了此类的对象。Class CMySocket{ CMySocket m_socket;//这个是不允许的,因为定义对象要分配空间,这个时候还不知道空间大小。 CMySocket* m_pSocket;//这个是可以的,因为指针空间大小为定值。}(2)在其它类中没有包含类的头文件,而是使用class CMySocket方式声明。class CMySocket;Class COther{ CMySocket m_socket;//这个是不允许的. CMySocket* m_pSocket;//这个是可以的} 把你的代码帖出来看看,就Socket而言,声明成指针或者一般变量都是可以的,是不是你在CMySocket中添加了其他特殊的成员变量。 #include "MyServerDlg.h"#include "afxsock.h"class CMyServerDlg;/////////////////////////////////////////////////////////////////////////////// CClientSock command targetclass CClientSock : public CSocket{// Attributespublic:// Operationspublic: CClientSock(CMyServerDlg* dlg); virtual ~CClientSock();// Overridespublic: CMyServerDlg* m_pDlg; // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClientSock) public: virtual void OnReceive(int nErrorCode); //}}AFX_VIRTUAL // Generated message map functions //{{AFX_MSG(CClientSock) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG// Implementationprotected:};CClientSock::CClientSock(CMyServerDlg* dlg):m_pDlg(dlg){}CClientSock::~CClientSock(){}// Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CClientSock, CSocket) //{{AFX_MSG_MAP(CClientSock) //}}AFX_MSG_MAPEND_MESSAGE_MAP()#endif // 0/////////////////////////////////////////////////////////////////////////////// CClientSock member functions//DEL void CClientSock::OnRecive()//DEL {//DEL //DEL }void CClientSock::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class CSocket::OnReceive(nErrorCode); //if(m_pdlg) //{ //}} OnOk OnCancel OnClose OnDestroy DestroyWindow?这些消息和函数的关系? 空间域的卷积和频域的卷积有什么不同? mfc中如何在view窗口中输出图片 ? 想学习图像处理 请都高手,关于线程与DLL问题 SDK VS JDK(那位解释一下这几个术语) 为什么结果不同?Linux下SOCKET网络通讯 debug窗口的东西怎么让它自动输出到一个文件中? 万分感谢,有关共享内存能不能解决malloc失败的问题 NTLDR是什么东东?谢谢您 快毕业了,想做个程序员,大家给点建议~ 如何自定一个通信协议?
(1)你在此类的声明中,定了了此类的对象。
Class CMySocket
{
CMySocket m_socket;//这个是不允许的,因为定义对象要分配空间,这个时候还不知道空间大小。
CMySocket* m_pSocket;//这个是可以的,因为指针空间大小为定值。
}(2)在其它类中没有包含类的头文件,而是使用class CMySocket方式声明。
class CMySocket;Class COther
{
CMySocket m_socket;//这个是不允许的.
CMySocket* m_pSocket;//这个是可以的
}
#include "MyServerDlg.h"
#include "afxsock.h"
class CMyServerDlg;
/////////////////////////////////////////////////////////////////////////////
// CClientSock command targetclass CClientSock : public CSocket
{
// Attributes
public:// Operations
public:
CClientSock(CMyServerDlg* dlg);
virtual ~CClientSock();// Overrides
public:
CMyServerDlg* m_pDlg; // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClientSock)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(CClientSock)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG// Implementation
protected:
};CClientSock::CClientSock(CMyServerDlg* dlg)
:m_pDlg(dlg)
{
}CClientSock::~CClientSock()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSock, CSocket)
//{{AFX_MSG_MAP(CClientSock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/////////////////////////////////////////////////////////////////////////////
// CClientSock member functions//DEL void CClientSock::OnRecive()
//DEL {
//DEL
//DEL }void CClientSock::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode);
//if(m_pdlg)
//{ //}
}