我在客户端clg.h头文件中引用了头文件“ClientSocket.h”,然后在客户端clg.h中的类中声明了类CClientSocket的对象,可是编译报错:
d:\vc++\客户端\客户端dlg.h(44) : error C2146: syntax error : missing ';' before identifier 'm_clientSocket'
d:\vc++\客户端\客户端dlg.h(44) : error C2501: 'CClientSocket' : missing storage-class or type specifiers
d:\vc++\客户端\客户端dlg.h(44) : error C2501: 'm_clientSocket' : missing storage-class or type specifiers
整了好久就是不知道哪边有问题,

 // 客户端Dlg.h : header file
// #if !defined(AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_)
#define AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 #include "AddrDlg.h"
#include "ClientSocket.h" /////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog class CMyDlg : public CDialog
{
// Construction
public:
CMyDlg(CWnd* pParent = NULL); // standard constructor
char m_ServerAddr[]; //IP地址 // Dialog Data
//{{AFX_DATA(CMyDlg)
enum { IDD = IDD_MY_DIALOG };
CListBox m_MsgR;
CEdit m_MsgS;
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL // Implementation
protected:
HICON m_hIcon;
int TryCount; //最大连接次数
CClientSocket m_clientSocket;
UINT m_Port; //端口号
// Generated message map functions
//{{AFX_MSG(CMyDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnConnect();
afx_msg void OnSend();
afx_msg void OnClose();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
}; //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_)

在你的对话框声明前面添加:

 class CClientSocket;

强制声明一下就 ok 了。
这个是 C++ 嵌套类定义检查的问题,在 include 之后要声明一下才能使用这个类。

我找到问题的根源了,我在客户端clg.h头文件中引用了头文件“ClientSocket.h”,又在ClientSocket.h中include了客户端clg.h。

还有,对于你的这种两个头文件互相包含的方式,是不好的做法,应该想办法改掉。

文章来源:http://www.dewen.org/q/9850

05-11 15:49