【问题标题】:Having trouble understanding this error creating a thread在创建线程时无法理解此错误
【发布时间】:2010-06-21 18:13:04
【问题描述】:
     HANDLE  hThread;
     DWORD   dwThreadId;

         hThread = CreateThread( 
     NULL,                   // default security attributes
     0,                      // use default stack size  
     MyThreadFunction,       // thread function name
     0,                      // argument to thread function 
     0,                      // use default creation flags 
     &dwThreadId);           // returns the thread identifier  <--Debugger takes me to this line?

错误指定了第三个参数,但是当我双击错误时,它会将我带到最后一个参数?
尝试运行 msdn CreateThread 示例http://msdn.microsoft.com/en-us/library/ms682453%28VS.85%29.aspx

error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void)' to 'unsigned long (__stdcall *)(void *)'
        None of the functions with this name in scope match the target type

【问题讨论】:

    标签: c++ windows multithreading winapi


    【解决方案1】:

    调试器只是将您带到语句的末尾。

    无论如何,你的函数签名是错误的,需要匹配函数指针类型。对于CreateThread,应该是:

    DWORD WINAPI ThreadProc(LPVOID lpParameter);
    

    【讨论】:

      【解决方案2】:

      您的函数签名与预期的签名不匹配。

      您的 MythreadFunction 函数应该返回 ULONG。

      类似:

      DWORD WINAPI MyThreadFunction(LPVOID lpParameter) {
      }
      

      【讨论】:

        【解决方案3】:

        点击错误会带你到最后一个参数,因为go-to-error函数只能通过statements走,整个函数调用就是一个statement。

        基本上,您的问题是MyThreadFunction 的签名错误。应该是unsigned long __stdcall MyThreadFunction(void*)(或等效的),但您写的是void MyThreadFunction(void)(或等效的)。

        【讨论】:

          【解决方案4】:

          当您双击错误时,它会显示错误发生的来源。由于函数调用表达式跨越多行,因此它将选择表达式的最后一行。

          问题是MyThreadFunction 没有正确的函数类型。 MyThreadFunction 是一个不带参数且不返回任何内容的函数。您需要将一个指针传递给一个函数,该函数接受一个参数(void*)并返回一个unsigned long

          【讨论】:

            猜你喜欢
            • 2014-09-28
            • 1970-01-01
            • 1970-01-01
            • 2017-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-01
            相关资源
            最近更新 更多