第一次发帖提问:书上小型聊天程序,编译出现的6d个错误

qingxin113 2008-04-10 03:10:25
一、出现的错误
D:\Programming\vc\TALK\MySocket.cpp(43) : error C2511: 'SetParent' : overloaded member function 'void (class CTALKDlg *)' not found in 'CMySocket'
d:\programming\vc\talk\mysocket.h(16) : see declaration of 'CMySocket'
D:\Programming\vc\TALK\MySocket.cpp(50) : error C2065: 'm_pDlg' : undeclared identifier
D:\Programming\vc\TALK\MySocket.cpp(50) : error C2227: left of '->OnAccept' must point to class/struct/union
D:\Programming\vc\TALK\MySocket.cpp(56) : error C2227: left of '->OnConnect' must point to class/struct/union
D:\Programming\vc\TALK\MySocket.cpp(61) : error C2227: left of '->OnClose' must point to class/struct/union
D:\Programming\vc\TALK\MySocket.cpp(66) : error C2227: left of '->OnReceive' must point to class/struct/union
二、MySocket.h代码
void CMySocket::SetParent(CTALKDlg *pDlg)//这里是第一个错误
{
m_pDlg=pDlg; //设置成员变量,但是不知道这里的m_pDlg是哪里的,没有申明过这个变量
}

void CMySocket::OnAccept(int nErrorCode)
{
if(nErrorCode==0)
m_pDlg->OnAccept();//调用CTALKDlg类中的OnAccept函数;且这里是第三个错误

}

void CMySocket::OnConnect(int nErrorCode)
{
m_pDlg->OnConnect();//这里是第四个错误
}

void CMySocket::OnClose(int nErrorCode)
{
m_pDlg->OnClose();//调用CTALKDlg类中的OnAccept函数,且这里是第五个错误
}

void CMySocket::OnReceive(int nErrorCode)
{
m_pDlg->OnReceive ();//调用CTALKDlg类中的OnClose函数,且这里是第六个错误
}
三、MySocket.h代码
class CMySocket : public CAsyncSocket//这里是第二个错误
{
// Attributes
public:

// Operations
public:
CMySocket();
virtual ~CMySocket();

// Overrides
public:
void SetParent(CWnd *pDlg);
void OnReceive(int nErrorCode);
void OnClose(int nErrorCode);
void OnConnect(int nErrorCode);
void OnAccept(int nErrorCode);
...全文
114 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lanmag 2008-12-23
照着本书打进去都一堆错误啊
回复
skyful123 2008-04-24
郁闷 出现问题的原因是你忘记其中的某几步

回去检查一下原因吧
回复
小二码农 2008-04-24
哥们,我也看了这本书,也做了这个例子,也遇到这么过问题,你的解决了吗
能否把最后的程序给我发一份 liangzhonglin@163.com
回复
qingxin113 2008-04-11
各位大哥大姐们,教教俺吧
回复
qingxin113 2008-04-10
而且已经在头文件中加了#include “TalkDlg.h”
回复
qingxin113 2008-04-10
void CTALKDlg::OnReceive()
{
//第十六步
char* pBuf=new char[1025];
int nBufSize=1024;
int nReceived;
CString strReceived;
nReceived=m_sConnectSocket.Receive(pBuf,nBufSize);//接收消息
if(nReceived!=SOCKET_ERROR)//判断是否接收成功
{
pBuf[nReceived]=NULL;//保留消息的有小部分
strReceived=pBuf;//将消息转为CString对象
m_listReceived.AddString(strReceived);//添加消息到列表框
UpdateData(false);
}
else
{
AfxMessageBox("信息发送错误",MB_OK|MB_ICONSTOP);
}

}

void CTALKDlg::OnClose()
{
//第十七步
m_sConnectSocket.Close();//关闭套接字
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(false);//禁用相关控件
GetDlgItem(IDOK)->EnableWindow(false);
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(false);
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(false);
GetDlgItem(IDC_BTN_CLOSE)->EnableWindow(false);
while(m_listSent.GetCount()!=0)
m_listSent.DeleteString(0); //清空列表框中的内容
while(m_listReceived.GetCount()!=0)
m_listReceived.DeleteString(0);
if(m_cmbType.GetCurSel()==1)//是否是客户机,是则激活相关控件,恢复到连接前状态
{
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(true);
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(true);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(true);
GetDlgItem(IDC_STATIC_SERVNAME)->EnableWindow(true);
GetDlgItem(IDC_STATIC_SERVPORT)->EnableWindow(true);
GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(true);
}

}

void CTALKDlg::OnBtnClose()
{
// TODO: Add your control notification handler code here,第十八步
OnClose();//调用OnClose函数

}
回复
qingxin113 2008-04-10
void CTALKDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTALKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTALKDlg::OnSelchangeComboType()
{
// TODO: Add your control notification handler code here,这是第3步,创建消息
UpdateData(true);
if(m_cmbType.GetCurSel()==0)//如果用户选择服务器,则变为"侦听"
m_btnConnect.SetWindowText("侦听");
else
m_btnConnect.SetWindowText("连接");//否则变为"连接"

}

void CTALKDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here,第十二步
UpdateData(true);
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(false);//禁用相关的控件
GetDlgItem(IDC_EDIT_SERVNAME)->EnableWindow(false);
GetDlgItem(IDC_EDIT_SERVPORT)->EnableWindow(false);
GetDlgItem(IDC_STATIC_SERVNAME)->EnableWindow(false);
GetDlgItem(IDC_STATIC_SERVPORT)->EnableWindow(false);
GetDlgItem(IDC_COMBO_TYPE)->EnableWindow(false);
if(m_cmbType.GetCurSel()==0)
{
m_sListenSocket.Create(m_nServPort);//若是服务器
m_sListenSocket.Listen();//开始侦听
}
else
{
m_sConnectSocket.Create(); //若是客户机
m_sConnectSocket.Connect(m_strServName,m_nServPort);//连接服务器
}


}

void CTALKDlg::OnAccept()
{
//第十三步
m_sListenSocket.Accept(m_sConnectSocket);//若是服务器,接受连接请求
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(true);//激活相关的控件
GetDlgItem(IDOK)->EnableWindow(true);
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(true);

}

void CTALKDlg::OnConnect()
{
//第十四步
//这里是否少了点什么
GetDlgItem(IDC_EDIT_MSG)->EnableWindow(true);//若是客户机,激活相关的控件
GetDlgItem(IDOK)->EnableWindow(true);
GetDlgItem(IDC_STATIC_MSG)->EnableWindow(true);
GetDlgItem(IDC_BTN_CLOSE)->EnableWindow(true);

}

void CTALKDlg::OnSendMsg()
{
// TODO: Add your control notification handler code here,第十五步
int nLen;//消息长度
int nSent;//已发送的消息长度
UpdateData(true);//将在控件中输入的数据赋给相应的变量
if(!m_strMsg.IsEmpty())
{
nLen=m_strMsg.GetLength();
nSent=m_sConnectSocket.Send(LPCTSTR(m_strMsg),nLen);//发送消息
if(nSent!=SOCKET_ERROR)
{
m_listSent.AddString(m_strMsg);//在列表框中添加消息
UpdateData(false);
}
else
{
AfxMessageBox("信息发送错误",MB_OK|MB_ICONSTOP);
}
m_strMsg.Empty();//清空当前消息
UpdateData(false);
}


}
回复
qingxin113 2008-04-10
void CTALKDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTALKDlg)
DDX_Control(pDX, IDC_BTN_CONNECT, m_btnConnect);
DDX_Control(pDX, IDC_LIST_SENT, m_listSent);
DDX_Control(pDX, IDC_LIST_RECEIVED, m_listReceived);
DDX_Control(pDX, IDC_COMBO_TYPE, m_cmbType);
DDX_Text(pDX, IDC_EDIT_MSG, m_strMsg);
DDX_Text(pDX, IDC_EDIT_SERVNAME, m_strServName);
DDX_Text(pDX, IDC_EDIT_SERVPORT, m_nServPort);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTALKDlg, CDialog)
//{{AFX_MSG_MAP(CTALKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType)
ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
ON_BN_CLICKED(IDOK, OnSendMsg)
ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTALKDlg message handlers

BOOL CTALKDlg::OnInitDialog()
{
{
m_cmbType.SetCurSel(1);//初始设置套接字类型为客户机
m_strServName="localhost";//初始为localhost
m_nServName=8000;//初始端口为8000//第十一个错误error C2065: 'm_nServName' : undeclared identifier
UpdateData(false);
m_sListenSocket.SetParent(this);//第十二个错误error C2660: 'SetParent' : function does not take 1 parameters
m_sConnectSocket.SetParent(this);//第十三个错误error C2660: 'SetParent' : function does not take 1 parameters
return true;
}
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

return TRUE; // return TRUE unless you set the focus to a control
}

void CTALKDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
回复
qingxin113 2008-04-10
三、TALKDlg.cpp代码:
// TALKDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TALK.h"
#include "TALKDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTALKDlg dialog

CTALKDlg::CTALKDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTALKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTALKDlg)
m_strMsg = _T("");
m_strServName = _T("");
m_nServPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
回复
qingxin113 2008-04-10
二、MySocket.h代码:
#if !defined(AFX_MYSOCKET_H__FD36C24C_57DD_47E3_A817_B66F1AB27675__INCLUDED_)
#define AFX_MYSOCKET_H__FD36C24C_57DD_47E3_A817_B66F1AB27675__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MySocket.h : header file
//



/////////////////////////////////////////////////////////////////////////////
// CMySocket command target

class CMySocket : public CAsyncSocket//这里报第三个错误see declaration of 'CMySocket'
{
// Attributes
public:

// Operations
public:
CMySocket();
virtual ~CMySocket();

// Overrides
public:
void OnReceive(int nErrorCode);
void OnClose(int nErrorCode);
void OnConnect(int nErrorCode);
void OnAccept(int nErrorCode);
void SetParent(CTALKDlg *pDlg);//第一个错误“error C2061: syntax error : identifier 'CTALKDlg'”//这里还有第九个错误error C2061: syntax error : identifier 'CTALKDlg'//第十个错误error C2061: syntax error : identifier 'CTALKDlg'
// 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:
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSOCKET_H__FD36C24C_57DD_47E3_A817_B66F1AB27675__INCLUDED_)
回复
qingxin113 2008-04-10
重新做了一遍,出来12个错误。郁闷中...
我把文件用记事本打开了,出现错误的代码如下,各位大大们帮帮忙再看看吧。
一、MySocket.cpp代码:
// MySocket.cpp : implementation file
//

#include "stdafx.h"
#include "TALK.h"
#include "MySocket.h"
#include "TalkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMySocket

CMySocket::CMySocket()
{
}

CMySocket::~CMySocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)
//{{AFX_MSG_MAP(CMySocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0

/////////////////////////////////////////////////////////////////////////////
// CMySocket member functions

void CMySocket::SetParent(CTALKDlg *pDlg)//这里报第二个错误error C2511: 'SetParent' : overloaded member function 'void (class CTALKDlg *)' not found in 'CMySocket'
{
//这是第6步创建的SetParent(CTALKDlg *pDlg)
m_pDlg=pDlg;//第七步,设置成员变量。
}

void CMySocket::OnAccept(int nErrorCode)
{
//第八步
if(nErrorCode==0)//这里报第五个错误error C2227: left of '->OnAccept' must point to class/struct/union
m_pDlg->OnAccept();//调用CTALKDlg类中的OnAccept函数,这里第四个错误error C2065: 'm_pDlg' : undeclared identifier
}

void CMySocket::OnConnect(int nErrorCode)
{
//第八步
m_pDlg->OnConnect();//这里报第六个错误error C2227: left of '->OnConnect' must point to class/struct/union
}

void CMySocket::OnClose(int nErrorCode)
{
//第八步
m_pDlg->OnClose();//这里报第七个错误error C2227: left of '->OnClose' must point to class/struct/union
}

void CMySocket::OnReceive(int nErrorCode)
{
//第八步
m_pDlg->OnReceive ();//这里报第八个错误error C2227: left of '->OnReceive' must point to class/struct/union
}
回复
skyful123 2008-04-10
#include <afxsock.h>
在MySocket.h加上面这句

然后自己 在public: 下面加一句CAsyncSocket *m_pDlg;
回复
先包含相应的头文件
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2008-04-10 03:10
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……