+ Reply to Thread
Results 1 to 2 of 2

Thread: Why Compiler throws an error when I use AfxBeginThread to create a thread?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    23

    Why Compiler throws an error when I use AfxBeginThread to create a thread?

    Why Compiler throws an error when I use AfxBeginThread to create a thread?

    Code:
    Code:
    class CMyThread
    {
    public:
    	UINT ThreadFunc(LPVOID lpVoid) ;
    	void StartThread() ;
    } ;
    
    UINT CMyThread::ThreadFunc(LPVOID lpVoid)
    {
    	AfxEndThread(0, TRUE) ;
    
    	return 0 ;
    }
    
    void CMyThread::StartThread()
    {
    	AfxBeginThread((AFX_THREADPROC) ThreadFunc, (LPVOID) this) ;
    }
    BOOL CMyDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    CMyThread objThread ;
    objThread.StartThread() ;

    return TRUE ;
    }

    Following is the error:

    "error C2440: 'type cast' : cannot convert from '' to 'unsigned int (__cdecl *)(void *)' None of the functions with this name in scope match the target type"

    Any reason?

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    27
    You are using non-static class method as your thread function which is not allowed!

    Following code can resolve your issue:

    Code:
    class CMyThread
    {
    public:
    	static UINT ThreadFunc(LPVOID lpVoid) ;//note here, we used static keyword!
    	void StartThread() ;
    } ;
    
    UINT CMyThread::ThreadFunc(LPVOID lpVoid)
    {
    	AfxEndThread(0, TRUE) ;
    
    	return 0 ;
    }
    
    void CMyThread::StartThread()
    {
    	AfxBeginThread((AFX_THREADPROC) ThreadFunc, (LPVOID) this) ;
    }
    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts