在基于对话框的程序中,加了个A类,,,在A类里声明.h中加个变量CClientDlg*  pdlg;,且包含了Dlg的头文件.我在对话框的OnInitDialog();把this给pdlg,好在A类里操作对话框,,,,,但是程序报错,说pdlg没声明,,,高手给讲讲吧.....咋回事........代码如下,CMySocket就是加的类.................-----------------------------------------------------------------------------------/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialogclass CClientDlg : public CDialog
{
// Construction
public:
CMySocket m_Client_Socket;
CClientDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CClientDlg)
enum { IDD = IDD_CLIENT_DIALOG };
CEdit m_SendEdit;
CEdit m_ReceiveEdit;
CIPAddressCtrl m_IP;
int m_SendType;
int m_ReceiveType;
int m_Port;
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClientDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CClientDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};----------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlersBOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
m_Port=2404;
m_IP.SetAddress(10,100,100,1);
m_ReceiveType=0;
m_SendType=0;
//m_Client_Socket.plg=(CClientDlg *)(this);
UpdateData(FALSE);
return TRUE;  // return TRUE  unless you set the focus to a control
}void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
----------------------------------------------------------------------------------------------#if !defined(AFX_MYSOCKET_H__45F56047_2B8C_4EC6_9888_0094B873EAED__INCLUDED_)
#define AFX_MYSOCKET_H__45F56047_2B8C_4EC6_9888_0094B873EAED__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MySocket.h : header file
//
#include "clientDlg.h"/////////////////////////////////////////////////////////////////////////////
// CMySocket command targetclass CMySocket : public CAsyncSocket
{
// Attributes
public:// Operations
public:
CMySocket();
virtual ~CMySocket();// Overrides
public:
CClientDlg*  pdlg;
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMySocket)
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(CMySocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG// Implementation
protected:
};
----------------------------------------------------------------------------------------------------------Configuration: client - Win32 Debug--------------------
Compiling...
client.cpp
e:\client\mysocket.h(26) : error C2143: syntax error : missing ';' before '*'
e:\client\mysocket.h(26) : error C2501: 'CClientDlg' : missing storage-class or type specifiers
e:\client\mysocket.h(26) : error C2501: 'pdlg' : missing storage-class or type specifiers
clientDlg.cpp
e:\client\mysocket.h(26) : error C2143: syntax error : missing ';' before '*'
e:\client\mysocket.h(26) : error C2501: 'CClientDlg' : missing storage-class or type specifiers
e:\client\mysocket.h(26) : error C2501: 'pdlg' : missing storage-class or type specifiers
MySocket.cpp
e:\client\clientdlg.h(19) : error C2146: syntax error : missing ';' before identifier 'm_Client_Socket'
e:\client\clientdlg.h(19) : error C2501: 'CMySocket' : missing storage-class or type specifiers
e:\client\clientdlg.h(19) : error C2501: 'm_Client_Socket' : missing storage-class or type specifiers
Generating Code...
Error executing cl.exe.client.exe - 9 error(s), 0 warning(s)

解决方案 »

  1.   

    m_Client_Socket.plg
    ================
    你这个是不是写错了
    按你帖子里说的是不是应该是m_Client_Socket.pdlg
      

  2.   

    //m_Client_Socket.plg=(CClientDlg *)(this);
    我都注释掉了,,,后编译的还是报错,,,
      

  3.   

    源码放网上了,,谁给编译一下看看,,,,,谢谢了.....
    http://e.ys168.com/note/fd.htm?http://ys-F.ys168.com/?client.rar_50c5bsm0e0ekkism0cp5bitlm1bspr1bs0b5bitlm0cp2bu14z97f14z
      

  4.   

    你在MySocket.h文件里面包含了clientDlg.h,又在clientDlg.h里面包含了MySocket.h。两个头文件互相包含就出现了你所遇到的错误。
    将MySocket.h文件里面的#include "clientDlg.h"移到MySocket.cpp文件里面就可以了。如果你想继续在MySocket.h里面使用CClientDlg*  pdlg;可以在前面加上 class CClientDlg;在头文件里面include其它头文件不是个什么好习惯,尽量在.cpp里面include别的头文件。
      

  5.   

    就算在cpp中引用也需要前置声明啊
      

  6.   

    可以换个思路,Socket类里不用dlg变量,而是用一个处理函数指针变量m_pFunc,然后添加一个设置处理函数的接口函数SetFunc(PROC * pFunc){m_pFunc=pFunc;}。
    在dlg里,调用SetFunc()指定一个全局的处理函数。
      

  7.   

    这样的问题肯定是头文件惹的祸,尽量在.cpp里包含头文件 如果.h里需要某个类型(比如类),单独声明,声明语句前后加上#ifndef #def #endif