Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing the member function pointer to the AfxBeginThread and its problems.

Passing the member function pointer to the AfxBeginThread and its problems.

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WindowsPistha
    wrote on last edited by
    #1

    Hi I am writing this to help others who will also have the same problem Creating worker thread : CWinThread *pThread = AfxBeginThread(&MyThread,(LPVOID*)ptrParam); MyThread is the member function of CWorker class, and its prototype is UINT MyThread( LPVOID pParam ) ; Now while compiling I am getting this error Error 4 error C2665: ‘AfxBeginThread’ : none of the 2 overloads could convert all the argument types We know the function prototype is correct. MSDN ref : UINT __cdecl MyControllingFunction( LPVOID pParam ); should be the function prototype. We are also having the Exact prototype , so what could be the problem. Solution : All the Class member function will have one hidden parameter ( ie ) this pointer. So Actucal function prototype of MyThread is UINT MyThread(CWorker pthis , LPVOID pParam ) ; becoz of this, compiler gives this error. To Solve the problem 1) Creating one global wrapper funciton around MyThread() UINT wrapper(LPVOID pParam) { CWorker *p = new(std::nothrow) CWorker ; if(!p) return ; p->MyThread(pParam); return 0; } CWinThread *pThread = AfxBeginThread(&wrapper,(LPVOID*)ptrParam); wil work perfectly. 2) Declaring the member function as static static UINT MyThread( LPVOID pParam ) ; Note: static member funciton cant access non static member function and non static data member. Regards, WindowsPistha

    D 1 Reply Last reply
    0
    • W WindowsPistha

      Hi I am writing this to help others who will also have the same problem Creating worker thread : CWinThread *pThread = AfxBeginThread(&MyThread,(LPVOID*)ptrParam); MyThread is the member function of CWorker class, and its prototype is UINT MyThread( LPVOID pParam ) ; Now while compiling I am getting this error Error 4 error C2665: ‘AfxBeginThread’ : none of the 2 overloads could convert all the argument types We know the function prototype is correct. MSDN ref : UINT __cdecl MyControllingFunction( LPVOID pParam ); should be the function prototype. We are also having the Exact prototype , so what could be the problem. Solution : All the Class member function will have one hidden parameter ( ie ) this pointer. So Actucal function prototype of MyThread is UINT MyThread(CWorker pthis , LPVOID pParam ) ; becoz of this, compiler gives this error. To Solve the problem 1) Creating one global wrapper funciton around MyThread() UINT wrapper(LPVOID pParam) { CWorker *p = new(std::nothrow) CWorker ; if(!p) return ; p->MyThread(pParam); return 0; } CWinThread *pThread = AfxBeginThread(&wrapper,(LPVOID*)ptrParam); wil work perfectly. 2) Declaring the member function as static static UINT MyThread( LPVOID pParam ) ; Note: static member funciton cant access non static member function and non static data member. Regards, WindowsPistha

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      There's nothing new about this. It's been discussed on this forum dozens of times, and there are even articles about it.

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      W 1 Reply Last reply
      0
      • D David Crow

        There's nothing new about this. It's been discussed on this forum dozens of times, and there are even articles about it.

        "Love people and use things, not love things and use people." - Unknown

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        W Offline
        W Offline
        WindowsPistha
        wrote on last edited by
        #3

        Yes you are right , I have red the below article before i posting. It did not talk about , why compiler gives error. so i thought of sharing here,I want to give something back to codeproject which gives lot of information to me. http://www.codeproject.com/KB/threads/memberthread.aspx[^] Regards, WindowsPistha

        D 1 Reply Last reply
        0
        • W WindowsPistha

          Yes you are right , I have red the below article before i posting. It did not talk about , why compiler gives error. so i thought of sharing here,I want to give something back to codeproject which gives lot of information to me. http://www.codeproject.com/KB/threads/memberthread.aspx[^] Regards, WindowsPistha

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          I searched and found three pages worth. Some of them include: http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1510191[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2166781[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2071901[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1363953[^]

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          W 1 Reply Last reply
          0
          • D David Crow

            I searched and found three pages worth. Some of them include: http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1510191[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2166781[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2071901[^] http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=1363953[^]

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            W Offline
            W Offline
            WindowsPistha
            wrote on last edited by
            #5

            Thank you Mr. David here after i will double check before i posting. Regards, WindowsPistha

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups