function - C++ Error on void type parameters -
i've got header:
class jmmvasync { public: static void run(lpctstr msg); };
and .cpp
void jmmvasync::run(lpctstr msg){ messagebox(null, msg, null, null); }
and i'm calling function:
lptcstr file = "file"; thread t(jmmvasync::run(file), 0);
thread function has structure:
thread::thread(void (*afunction)(void *), void * aarg)
why getting wrong types when calling "thread"?
error code: compile : error c2664: 'tthread::thread::thread(void (__cdecl *)(void *),void *)' : cannot make conversion of parameter 1 type 'void' 'void (__cdecl *)(void *)'
thread
function expects paramater 1 void (__cdecl *)(void *)
, function void
. don't know how make function named run
same type requested.
as mentioned in comments (but maybe in unclear fashion), code tries pass return value of function, instead of pointer function.
this constructor
thread::thread(void (*afunction)(void *), void * aarg)
expects pointer function first argument.
this
jmmvasync::run(file)
invokes function , result function's return value (which void
). absolutely not want. want address of function:
&jmmvasync::run
send constructor way:
lptcstr file = "file"; thread t(&jmmvasync::run, file);
note: second parameter file
, not 0
. common pattern in c: pass address of function , parameter, of type void*
, , library code promises call function later, passing parameter it.
btw aaron mcdaid mentions, type of jmmvasync::run
must constructor requests. is, must declared receive parameter of type void*
(not lptcstr
), , static
member function (which is, judging code). since using names lptcstr
, want code run on windows, don't need worry distinction between void*
, lptcstr
.
if in doubt, make wrapper:
void wrapper_jmmvasync_run(void* par) { jmmvasync::run(static_cast<lptcstr>(file)); } ... lptcstr file = "file"; thread t(&wrapper_jmmvasync_run, file);
Comments
Post a Comment