我在客戶端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
整了好久就是不知道哪邊有問題,
1 // 客戶端Dlg.h : header file 2 // 3 4 #if !defined(AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_) 5 #define AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_ 6 7 #if _MSC_VER > 1000 8 #pragma once 9 #endif // _MSC_VER > 1000 10 11 #include "AddrDlg.h" 12 #include "ClientSocket.h" 13 14 ///////////////////////////////////////////////////////////////////////////// 15 // CMyDlg dialog 16 17 class CMyDlg : public CDialog 18 { 19 // Construction 20 public: 21 CMyDlg(CWnd* pParent = NULL); // standard constructor 22 char m_ServerAddr[256]; //IP地址 23 24 // Dialog Data 25 //{{AFX_DATA(CMyDlg) 26 enum { IDD = IDD_MY_DIALOG }; 27 CListBox m_MsgR; 28 CEdit m_MsgS; 29 //}}AFX_DATA 30 31 // ClassWizard generated virtual function overrides 32 //{{AFX_VIRTUAL(CMyDlg) 33 protected: 34 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 35 //}}AFX_VIRTUAL 36 37 // Implementation 38 protected: 39 HICON m_hIcon; 40 int TryCount; //最大連接次數 41 CClientSocket m_clientSocket; 42 UINT m_Port; //端口號 43 // Generated message map functions 44 //{{AFX_MSG(CMyDlg) 45 virtual BOOL OnInitDialog(); 46 afx_msg void OnSysCommand(UINT nID, LPARAM lParam); 47 afx_msg void OnPaint(); 48 afx_msg HCURSOR OnQueryDragIcon(); 49 afx_msg void OnConnect(); 50 afx_msg void OnSend(); 51 afx_msg void OnClose(); 52 afx_msg void OnTimer(UINT nIDEvent); 53 //}}AFX_MSG 54 DECLARE_MESSAGE_MAP() 55 }; 56 57 //{{AFX_INSERT_LOCATION}} 58 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 59 60 #endif // !defined(AFX_DLG_H__65522289_1F04_4B8E_AA6C_FDFD80E5CF71__INCLUDED_)
在你的對話框聲明前面添加:
1 class CClientSocket;
強制聲明一下就 ok 了。
這個是 C++ 嵌套類定義檢查的問題,在 include 之后要聲明一下才能使用這個類。
我找到問題的根源了,我在客戶端clg.h頭文件中引用了頭文件“ClientSocket.h”,又在ClientSocket.h中include了客戶端clg.h。
還有,對於你的這種兩個頭文件互相包含的方式,是不好的做法,應該想辦法改掉。
文章來源:http://www.dewen.org/q/9850