yycGIS

我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容。 我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系。

Less is more, more is different!

导航

做MFC的时候引用了头文件但VC还是报missing storage-class or type specifiers

我在客户端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

 

posted on 2014-05-08 22:26  yycGIS  阅读(5526)  评论(0编辑  收藏  举报