error C2440: 'initializing' : cannot convert from 'class CMySocket *' to 'class CMySocket' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(231) : error C2065: 'g_pMySocket' : undeclared identifier C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(231) : error C2227: left of '->m_socket' must point to class/struct/union C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(252) : error C2059: syntax error : 'static ' C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(253) : error C2039: 'g_pMySocket' : is not a member of 'CChatTcpDlg' c:\documents and settings\administrator\桌面\chattcp\chattcp\chattcpdlg.h(23) : see declaration of 'CChatTcpDlg' C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(253) : error C2227: left of '->OnAccpet' must point to class/struct/union C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(260) : error C2065: 'MySocket' : undeclared identifier C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(260) : error C2228: left of '.OnClose' must have class/struct/union type C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(266) : error C2228: left of '.OnSend' must have class/struct/union type C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(272) : error C2228: left of '.OnReceive' must have class/struct/union type C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(278) : error C2228: left of '.OnConnect' must have class/struct/union type Error executing cl.exe.
你的全局变量在 没有AfxSockInit()之前就创建套接字,当然失败了.
看错误的那个不会用.
UINT AcceptThread(LPVOID pParam )
{
CChatTcpDlg* pdlg = static<CChatTcpDlg*>(pParam);
pdlg->g_pMySocket->OnAccpet(pParam);
AfxEndThread(TRUE);
return 1;
}
这里把this传递到线程体内.所有的初始化工作等dlg创建后在开始.
CMySocket不能用全局,在工程没有创建socket环境之前,要不用指针,在dlg里面new
eg:
BOOL CChatTcpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
g_pMySocket = new CMySocket();
}
还有,干吗进入线程体内就要EndThread呢???不明白你的意思,大致错误就这些了.
不喜欢你的软件架构..
class CBaseProcess
{
protected:
CBaseProcess();
virtual ~CBaseProcess(void);public:
char m_precvbuf[UDP_MAX_PACKET];
private: HANDLE m_hRecvThread;
HANDLE m_hSendThread; CLock m_hLock; CYLTongDlg* m_pDlg; std::queue<s_struct*> m_dequeMsg;
public:
static unsigned int RecvFunc(LPVOID lParam); //接收线程
static unsigned int SendFunc(LPVOID lParam); //发送线程
public:
virtual int DoRecvWork(LPVOID lParam)=0; //接收工作
virtual int DoSendWork(LPVOID lParam)=0; //发送工作
// 将底层数据处理层接收到的消息抛出到用户的应用层
void PostTask(int ntype,unsigned int ulen, char* pbuf);
// 接收发送任务,由用户应用层投递给数据处理层
void PushTask(unsigned int ulen, char* pbuf);
// 取一个发送任务
s_struct* GetNextTask(void);
};
#include "StdAfx.h"
#include ".\baseprocess.h"
#include "YLTongDlg.h"
CBaseProcess::CBaseProcess()
{
m_hRecvThread = ::CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)&RecvFunc,this,NULL,NULL);
m_hSendThread = ::CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)&SendFunc,this,NULL,NULL);
}CBaseProcess::~CBaseProcess(void)
{
}
unsigned int CBaseProcess::RecvFunc(LPVOID lParam)
{
CBaseProcess* pProcess=(CBaseProcess*)lParam;
return pProcess->DoRecvWork(NULL);
}
unsigned int CBaseProcess::SendFunc(LPVOID lParam)
{
CBaseProcess* pProcess=(CBaseProcess*)lParam;
return pProcess->DoSendWork(NULL);
}
// 将地层数据处理层接收到的消息抛出到用户的应用层
void CBaseProcess::PostTask(int ntype ,unsigned int ulen, char* pbuf)
{
char* pmsg = new char[ulen];
memcpy(pmsg,pbuf,ulen);
if(ntype==0)
m_pDlg->PostMessage(DATA_UDP_MSG,(WPARAM)pbuf,ulen);
else
m_pDlg->PostMessage(DATA_TCP_MSG,(WPARAM)pbuf,ulen);
}// 接收发送任务,由用户应用层投递给数据处理层,加入到发送队列
void CBaseProcess::PushTask(unsigned int ulen, char* pbuf)
{
s_struct *psbuf = new s_struct;
psbuf->pbuf = new char[ulen];
memcpy(psbuf,pbuf,ulen);
psbuf->nlen = ulen; m_hLock.Lock();
m_dequeMsg.push(psbuf);
m_hLock.UnLock();
}// 取一个发送任务
s_struct* CBaseProcess::GetNextTask(void)
{
s_struct* pInfo=NULL;
m_hLock.Lock();
if(!m_dequeMsg.empty())
{
pInfo=m_dequeMsg.front();
m_dequeMsg.pop();
}
m_hLock.UnLock();
return pInfo;
}
int CUdpProcess::DoRecvWork(LPVOID lParam)
{
while(true)
{
try
{
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {5, 0}; //1分钟
int nRet = select(0, &fd, NULL, NULL, &tv);
if( nRet == 1)
{
memset(m_precvbuf,0,UDP_MAX_PACKET);
SOCKADDR_IN addr;
memset(&addr,0,sizeof(addr));
int naddrlen=sizeof(addr);
int nRecv = recvfrom(m_hSocket,(char*)m_precvbuf,UDP_MAX_PACKET,0,
(sockaddr*)&addr,&naddrlen);
if(nRecv > 1) PostTask(0,nRecv,m_precvbuf);
}
}
catch(char* pMsg)
{
TRACE0(pMsg);
//std::cout << "Error " << __FILE__ << __LINE__ << pMsg << std::endl;
}
//这里接收信息,然后交给处理线程
}
}
No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(231) : error C2065: 'g_pMySocket' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(231) : error C2227: left of '->m_socket' must point to class/struct/union
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(252) : error C2059: syntax error : 'static '
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(253) : error C2039: 'g_pMySocket' : is not a member of 'CChatTcpDlg'
c:\documents and settings\administrator\桌面\chattcp\chattcp\chattcpdlg.h(23) : see declaration of 'CChatTcpDlg'
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(253) : error C2227: left of '->OnAccpet' must point to class/struct/union
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(260) : error C2065: 'MySocket' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(260) : error C2228: left of '.OnClose' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(266) : error C2228: left of '.OnSend' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(272) : error C2228: left of '.OnReceive' must have class/struct/union type
C:\Documents and Settings\Administrator\桌面\ChatTcp\ChatTcp\ChatTcpDlg.cpp(278) : error C2228: left of '.OnConnect' must have class/struct/union type
Error executing cl.exe.
刚才改了大半天,机器重启了,够郁闷的.
反正你不是挺闲的,就帮我改改呗.