create multi-thread - c++

I use Multi-thread method in vs2008 ,use c++ language. when I use _beginthreadex function, I got the follow error:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int
(__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'
ps: I use the template on the threadFunc like this:
template<class T>
unsigned int WINAPI closingReconstruction_ThreadFunc(void* pvPara)
{...}
,and then i creat the thread
_beginthreadex(NULL,0,closingReconstruction_ThreadFunc<T>,(PVOID)(tPara+i),0,NULL)
so i get this error , of course , if I remove the template ,it can be work,but i need to use template, are there any methods to solve this.

You forgot to mention the immediately preceding error message,
error C2065: 'T' : undeclared identifier
Let me just state that looking at the first error message first, is generally a good idea.
Cheers & hth.,

Related

What is the correct function pointer for an unsigned WINAPI function?

I've a function declared in this way:
unsigned WINAPI searchSTR(void *j);
And I need a pointer to this function. My idea was:
unsigned (*pointerF) (void*);
pointerF = &searchSTR;
But there is an error:
"1 error C2440: '=' : cannot convert from 'unsigned int (__stdcall *)(void *)'
to 'unsigned int (__cdecl *)(void *)' ".
I tried other sintax, but nothing seems correct, he doesn't like the word WINAPI.
Can you suggest me the correct syntax? Maybe it is easy but I am blocked ! Thanks to all
The WINAPI macro expands to __stdcall, which is a different calling convention from the default __cdecl. You need to mark your function pointer with the calling convention to use:
unsigned (WINAPI *pointerF) (void*)
auto pointerF = &searchSTR;
Stop writing 1990's code. The compiler already knows the right type.

C++ Defining function pointer

I am trying to create a function pointer to another function in c++.
This is what I have so far:
LONG (*function)(LPSTR,LPVIPERVAR4,LONG)=&CWilExtender::DllVarHandler;
When I try to compile my program, I get this error:
.\MyExtender.cpp(132) : error C2440: 'initializing' : cannot convert from
'LONG (__thiscall CWilExtender::* )(LPSTR,LPVIPERVAR4,LONG)' to
'LONG (__cdecl *)(LPSTR,LPVIPERVAR4,LONG)'
There is no context in which this conversion is possible
I don't know how the DllVarHandler was defined, and I don't know how to reproduce the type for the function pointer.
How do I change the (_cdecl *) to match (__thisscall CWilExtender::*)?
Specifically, what does LONG (__thiscall CWilExtender::* )(LPSTR,LPVIPERVAR4,LONG) mean and how do I write that as the function pointer's type?
Thanks.
Thanks to the comments by #OliCharlesworth and #user814628, I solved my problem.
The correct code should be:
LONG (CWilExtender::* function)(LPSTR,LPVIPERVAR4,LONG)=&CWilExtender::DllVarHandler;
Thanks for being so quick to help!

Converting from var to var&

How can I convert a variable of type var or var* to var&
I've to use a function which takes an object of var class(suppose there's a class). The example code was given like this:-
testFunc(false, namespace1::namespace2::var(), 100);
in function declaration it says that the second parameter is of type namespace1::namespace2::var&, I can create namespace1::namespace2::var or namespace1::namespace2::var*, but how do I create namespace1::namespace2::var&?
I know that's too basic question but I couldn't figure it out.
Edit:
I've tried using just var, but it gives some odd errors. I am pretty sure that's some kind of fault in the function I am using. Here're the errors:-
Error 3 error C2825: 'CType': must be a class or namespace when followed by '::'
Error 4 error C2039: 'TypeCode' : is not a member of '`global namespace''
Error 5 error C2146: syntax error : missing ',' before identifier 'TypeCode'
Error 6 error C2065: 'TypeCode' : undeclared identifier
Error 7 error C3203: 'CustomType' : unspecialized class template can't be used as a template argument for template parameter 'Base', expected a real type
Edit 2
I thought it'd be hard to answer if I include real code, since it's complicated. But have a look if it helps. The real function's signature is like this:-
virtual bool opRaiseEvent(bool reliable, const Common::Hashtable& parameters, nByte eventCode, nByte channelID=0, int* targetPlayers=NULL, short numTargetPlayers=NULL);
and the example code used the function like this:-
mLoadBalancingClient.opRaiseEvent(false, ExitGames::Common::Hashtable(), 100);
which was working fine. But now I want to add data to the HashTable, so I need to create an object of it and then pass it to the function. It's not accepting a pointer or normal variable. I don't know why it's working with just HashTable().
It means the second parameter is passed by reference. You have to simply pass:
namespace1::namespace2::var
This should work :
const Common::Hashtable param = namespace1::namespace2::var();
opRaiseEvent(false, param, 100);
namespace1::namespace2::var v;
testFunc(false, v, 100);

_begintheadex function call problem

I have a class SoundManager which contains a function called 'recordLoop'. In the constructor of the SoundManager, I am using this code:
recordHandle = (HANDLE)_beginthreadex(NULL,0,recordLoop,
(void*)exinfo->length,CREATE_SUSPENDED,0);
It is giving me the following errors:
error C3867: 'SoundManager::recordLoop': function call missing argument list; use '&SoundManager::recordLoop' to create a pointer to member
IntelliSense: argument of type "unsigned int (__stdcall SoundManager::*)(void *params)" is incompatible with parameter of type "unsigned int (__stdcall *)(void *)"
So I've tried using the &SoundManager::recordLoop as suggested, but it gives me this:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall SoundManager::* )(void *)' to 'unsigned int (__stdcall *)(void *)'
IntelliSense: argument of type "unsigned int (__stdcall SoundManager::*)(void *params)" is incompatible with parameter of type "unsigned int (__stdcall *)(void *)"
Is it illegal to start a thread on a class method or did I do something wrong?
Thanks in advance
EDIT: Sorry forgot to add the recordLoop >.< here it is:
public:
unsigned __stdcall recordLoop(void* params);
It's illegal to start a thread on a non-static class member since there is no way for the created thread to know what this is.
What is the definition of recordLoop?
I had the same problem with casting.
Ignoring all other problems like one mentioned in the answer above, function pointer must be cast to (unsigned(__stdcall*)(void*)) in _beginthreadex, no matter what type the function is or what is its parameter list.

cvblob compile error in Visual C++ 6.0

I'm using Microsoft Visual C++ 6.0 and Microsoft Visual Studio 2008 to develop an academic computer vision project.
In this project i need to use OpenCV 1.1 (http://opencv.willowgarage.com/) and CvBlob (http://code.google.com/p/cvblob/).
I tried to compile this project with Microsoft Visual Studio 2008 and it compiles without errors.
With Visual C++ 6.0 i got a lot of errors.
OpenCV are not responsible of this behavior, because a trivial project with only OpenCV (without CvBlob) works well.
To understand the errors better I made an empty project with only the CvBlob inclusion.
I paste here a brief summary of the errors:
cvcontour.cpp(253) : error C2371: 'i' : redefinition; different basic types (and others similar to this. i solved with variable redefinition, every time)
cvcontour.cpp(318) : error C2664: 'thiscall std::vector<struct CvPoint,class std::allocator<struct CvPoint> >::std::vector<struct CvPoint,class std::allocator<struct CvPoint> >(unsigned int,const struct CvPoint &,const class std::allocator<struct CvPoint> &)' : cannot convert parameter 1 from 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' to 'unsigned int' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
cvtrack.cpp(278) : error C2440: 'initializing' : cannot convert from 'struct cvb::CvTrack *const ' to 'struct cvb::CvBlob *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Have you ideas on how can i solve these problems?
Thanks in advance for the help!
-------- UPDATE --------
I tried to edit and correct the code in order to elminate the three errors in my question.
The error C2664 seems to be the more difficult to cirmumvent...
I have replaced the indicted line
return new CvContourPolygon(dq.begin(), dq.end());
where CvContourPolygon is a typedef std::vector<CvPoint> CvContourPolygon;
with
deque<int>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
v_tmp.push_back(*dq_it);
while (dq_it != dq.end()){
v_tmp.push_back(*dq_it++);
}
First, what that i wrote is correct? Than, how can i solve the errors that occured from this?
Thank you in advance!
Errors (suppose that the first line is 318:
cvcontour.cpp(319) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or
there is no acceptable conversion)
cvcontour.cpp(321) : error C2664: 'push_back' : cannot convert parameter 1 from 'int' to 'const struct CvPoint &'
Reason: cannot convert from 'int' to 'const struct CvPoint'
No constructor could take the source type, or constructor overload resolution was ambiguous
cvcontour.cpp(322) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or there is no acceptable conversion)
cvcontour.cpp(322) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
-------- UPDATE2 --------
This code seems to work correctly!
deque<CvPoint>::iterator dq_it;
dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it){
v_tmp.push_back(*dq_it);
}
//return new CvContourPolygon(dq.begin(), dq.end());
return &v_tmp;
C2371 - VC6 was sloppy with scope of local variables. Should be able to fix this by making the code use variable names unambiguously.
C2664 - looks like failure to initialize a vector using deque iterators - wrong overload on vector::vector() being called? Probably have to work around this by manually copying the deque elements to the new vector somehow.
C2440 - check the objects are compatible (VS2008 seems to think so) and add the appropriate cast.
EDIT:
Shouldn't your code look like this?
deque<CVPoint>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it)
{
v_tmp.push_back(*dq_it);
}