写完之后,有点错误,错误排除

starrychat 2011-12-01 03:09:23
"CChatDlg"类。

...............
BEGIN_MESSAGE_MAP(CChatDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_RECVDATA,OnRecvData)
错误一:Error 1 error C2440: 'static_cast' : cannot convert from 'void (__thiscall CChatDlg::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 68 1 Chat
什么原因造成的,怎样修改???

错误六: 6 IntelliSense: invalid type conversion c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 68 2 Chat
什么原因造成的,怎样修改???

// ON_MESSAGE(WMUSER+1, &CChatDlg::WM_RECVDATA)
ON_BN_CLICKED(IDC_BTN_SEND, &CChatDlg::OnBnClickedBtnSend)
END_MESSAGE_MAP()


// CChatDlg message handlers

BOOL CChatDlg::OnInitDialog()
{
CDialogEx::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)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
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
//初始化套接字
InitSocket();

//创建接收线程
RECVPARAM *pRecvParam=new RECVPARAM;
pRecvParam->sock=m_socket;
pRecvParam->hwnd=m_hWnd;
HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
//关闭该接收程句柄,释放其引用计数
CloseHandle(hThread);

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


}

void CChatDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::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.

void CChatDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CDialogEx::OnPaint();
}
}

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

bool CChatDlg::InitSocket(void)
{
//创建套接字
m_socket=socket(AF_INET,SOCK_DGRAM,0);
if (INVALID_SOCKET==m_socket)
{
MessageBox(_T("套接字创建失败!"));
return FALSE;

SOCKADDR_IN addrSock;
addrSock.sin_family=AF_INET;
addrSock.sin_port=htons(6000);
addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);


int retval;
//绑定套接字
retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
if (SOCKET_ERROR==retval)
{
closesocket(m_socket);
MessageBox(_T("绑定失败!"));
return FALSE;
}
return TRUE;
}

DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)
{
错误二:“{”提示错误如下:
Error 2 error C2601: 'CChatDlg::RecvProc' : local function definitions are illegal c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 199 1 Chat
什么原因,怎样修改???

错误七:“RecvProc”提示错误,信息如下:
7 IntelliSense: member function "CChatDlg::RecvProc" may not be redeclared outside its class c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 198 24 Chat

错误八:“{”提示错误,信息如下:
8 IntelliSense: expected a ';' c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 199 1 Chat

什么原因,怎样修改???

//获取主线程传递的套接字和窗口句柄
SOCKET sock=((RECVPARAM*)lpParameter)->sock;
HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
delete lpParameter;


SOCKADDR_IN addrFrom;
int len=sizeof(SOCKADDR);

char recvBuf[200];
char tempBuf[300];
int retval;
while(TRUE)
{
//接收数据
retval=recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
if (SOCKET_ERROR==retval)
{
break;
sprintf(tempBuf,"%s说%s",inet_ntoa(addrFrom.sin_addr),recvBuf);
::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);

}

}

return 0;
}

//接收数据消息响应函数
void CChatDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
{
错误三:“{”提示错误,信息如下:
Error 3 error C2601: 'CChatDlg::OnRecvData' : local function definitions are illegal c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 231 1 Chat
什么原因造成的,怎样修改???

//取出接收到的数据
CString str=(char*)lParam;
CString strTemp;

//获得已有数据
GetDlgItemText(IDC_EDIT_RECV,strTemp);
str+="\r\n";
str+=strTemp;
//显示所有接收到的数据
SetDlgItemText(IDC_EDIT_RECEV,str);
}


void CChatDlg::OnBnClickedBtnSend()
{
错误四:“{”提示错误,信息如下:
Error 4 error C2601: 'CChatDlg::OnBnClickedBtnSend' : local function definitions are illegal c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 301 1 Chat
什么原因???怎么“{”提示错误呢???

// TODO: Add your control notification handler code here
//获取对方IP
DWORD dwIP;
((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);

SOCKADDR_IN addrTo;
addrTo.sin_family=AF_INET;
addrTo.sin_port=htons(6000);
addrTo.sin_addr.S_un.S_addr=htonl(dwIP);


CString strSend;
//获得待发送数据
GetDlgItemText(IDC_EDIT_SEND,strSend);
//发送数据
sendto(m_socket,strSend,strSend.GetLength()+1,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
//清空发送编辑框中的内容
SetDlgItemText(IDC_EDIT_SEND,"");
}
//最后一行的外面
问题五:最后一行的外面提示错误,信息如下;
Error 5 error C1075: end of file found before the left brace '{' at 'c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp(172)' was matched c:\users\administrator.pc-20110905onbv\desktop\demo\vc\dialog\chat\chat\chatdlg.cpp 321 1 Chat
什么原因???怎样修正???


这些错误是什么原因造成的呢?怎样修改???
...全文
287 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
starryplayer 2011-12-06
  • 打赏
  • 举报
回复
“LRESULT”这是什么呀???
gold_water 2011-12-02
  • 打赏
  • 举报
回复
VS2010消息函数返回值必须为LRESULT类型。
凌乱哥 2011-12-02
  • 打赏
  • 举报
回复
少了一个大括号,整个cpp文件的结构就都乱了
Eleven 2011-12-01
  • 打赏
  • 举报
回复
这个“#define WM_RECVDATA WM_USER+1”的作用是什么呢???
---------------
自定义消息
starryplayer 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 andywei1982 的回复:]

给你个怎么使用自定义消息的例子吧:
#define WM_RECVDATA WM_USER+1
这个最好定义在一个公共的头文件,因为发消息的地方要用到
头文件里面
afx_msg LRESULT OnRecvData(WPARAM wParam ,LPARAM lParam);
cpp文件里面
ON_MESSAGE(MSG_HAVEDATA,OnHavaDate)

然后实现
……
[/Quote]

这个“#define WM_RECVDATA WM_USER+1”的作用是什么呢???

[color=#FF0000]查出来了,是“}”的错误。
为什么一个“}”错误,会衍生出那么多的其它类型的错误呢???

[/color]
andywei1982 2011-12-01
  • 打赏
  • 举报
回复
给你个怎么使用自定义消息的例子吧:
#define WM_RECVDATA WM_USER+1
这个最好定义在一个公共的头文件,因为发消息的地方要用到
头文件里面
afx_msg LRESULT OnRecvData(WPARAM wParam ,LPARAM lParam);
cpp文件里面
ON_MESSAGE(MSG_HAVEDATA,OnHavaDate)

然后实现
LRESULT CImProDlg::OnRecvData(WPARAM wParam ,LPARAM lParam)
{

//函数实现这个地方和你怎么发消息对应的。。例如
//定义的消息结构体如下
//struct MSGBODY
//{
// string strUserID;
// string strmyid;
// string strMsg;
//};
//这样发送消息

MSGBODY msgbody;
msgbody.strmyid = string(strmyid);
msgbody.strUserID = string(userID);
msgbody.strMsg = string(msg);
::SendMessage(m_wnd,MSG_HAVEDATA,(WPARAM)&msgbody,0);
//
//

MSGBODY *body = (MSGBODY*)wParam;

CString strMsg,strUserID,strMyid;
strMsg.Format("%s",body->strMsg.c_str());
strUserID.Format("%s",body->strUserID.c_str());
strMyid.Format("%s",body->strmyid.c_str());
return true;
}
starrychat 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dingxz105090 的回复:]

首先,这是孙鑫的教程里的吧,你肯定是哪里不一致,不然照着他的教程做下来是不会有问题的

其次,#define WM_RECVDATA WM_USER+1//这个应该写在头文件的开头处,而且,一般自定义的消息都要定义到WM_USER+100以后,也就是说比如WM_USER+101、WM_USER+102等等;

还有这个afx_msg void OnRecvData(WPARAM wPar……
[/Quote]

这是“孙鑫”教程里面的,想研究下多线程。是照着做的呢,怎么会提示那么多的错误。
唯一不同的就是开发环境,小弟用的是VS2010....
dhbfly 2011-12-01
  • 打赏
  • 举报
回复
头文件 添加这个 试试DECLARE_DYNAMIC(CChatDlg )
凌乱哥 2011-12-01
  • 打赏
  • 举报
回复
首先,这是孙鑫的教程里的吧,你肯定是哪里不一致,不然照着他的教程做下来是不会有问题的

其次,#define WM_RECVDATA WM_USER+1//这个应该写在头文件的开头处,而且,一般自定义的消息都要定义到WM_USER+100以后,也就是说比如WM_USER+101、WM_USER+102等等;

还有这个afx_msg void OnRecvData(WPARAM wParam,LPARAM lParam);不是ClassWizard添加的消息映射最好放在DECLARE_MESSAGE_MAP()外面
starrychat 2011-12-01
  • 打赏
  • 举报
回复
对应头文件如下:

#pragma once


// CChatDlg dialog
class CChatDlg : public CDialogEx
{
// Construction
public:
CChatDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
enum { IDD = IDD_CHAT_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support


// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnRecvData(WPARAM wParam,LPARAM lParam);
DECLARE_MESSAGE_MAP()
private:
SOCKET m_socket;
public:
// static bool TestMeth(int x);

// char TestDlg(char w, float y)
// {
// return 0;
// }
// virtual float TestDlg(float x, float y);
// int TestMt(int b) const;

bool InitSocket(void);


static DWORD WINAPI RecvProc(LPVOID lpParameter);

protected:
// afx_msg LRESULT WM_RECVDATA(WPARAM wParam, LPARAM lParam);

#define WM_RECVDATA WM_USER+1
public:
afx_msg void OnBnClickedBtnSend();
};

struct RECVPARAM
{
SOCKET sock;//已创建的套接字
HWND hwnd;//对话框句柄
};

小弟不甚感激,百拜。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧