zoukankan      html  css  js  c++  java
  • 做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

  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/qiernonstop/p/3717808.html
Copyright © 2011-2022 走看看