How do I cast class method to QScriptEngine::FunctionSignature - c++

I want to pass this method:
QScriptValue ScriptProcessContext::construct(QScriptContext * ctx, QScriptEngine *)
{
return this->newInstance();
}
to QScriptEngine::newFunction. I tried the following options:
As in documentation and also this example code:
QScriptValue ctor = engine->newFunction(construct);
Error:
error C3867: 'ScriptProcessContext::construct': function call missing argument list; use '&ScriptProcessContext::construct' to create a pointer to member
Force the cast:
QScriptValue ctor = engine->newFunction((QScriptEngine::FunctionSignature)construct);
Error:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'QScriptEngine::FunctionSignature'
Cast the pointer to member
QScriptValue ctor = engine->newFunction(
(QScriptValue(ScriptProcessContext::*)(QScriptContext *, QScriptEngine *))
&ScriptProcessContext::construct
);
Error:
error C2664: 'QScriptValue QScriptEngine::newFunction(QScriptEngine::FunctionSignature,int)' : cannot convert parameter 1 from 'QScriptValue (__cdecl ScriptProcessContext::* )(QScriptContext *,QScriptEngine *)' to 'QScriptEngine::FunctionSignature'
So how to write it correctly?

You try to pass a memberfunction. That is not supported. Use a free function or a static method.
Note that in both cases, you won't have a this pointer. You need to write the function in a way that it doesn't need an object.

Related

A const member function can not deduce parameter type

Here is the code:
vector<ClientInfo*> OpenRABiz::GetHumans() const {
vector<ClientInfo*> vec;
for (auto &c : clients) {
if (!c.isbot) {
vec.push_back(&c);
}
}
return vec; // RVO - return value optimization
}
In visual c++ 2019, compiler indate it:
error C2664: 'void std::vector<ClientInfo *,std::allocator<ClientInfo *>>::push_back(_Ty &&)': cannot convert argument 1 from 'const ClientInfo *' to 'const _Ty &'
The error message: "
const _Ty&" means the c++ template can't deduce the right parameters
.
When I take the const keyword, it compiles successfully.
vector<ClientInfo*> OpenRABiz::GetHumans()
Why?
Your clients is likely a vector of ClientInfo, so in a const-qualified member-functions, the type of client (in the loop) is const ClientInfo&. When you take the address &client, you get a const ClientInfo*, which cannot be converted to a ClientInfo*.
When you remove the const-qualifier, everything works fine because client is then ClientInfo&.
To fix the issue, change the return-type of the function and declaration of vec to std::vector<const ClientInfo*>.

gluTessCallback error C2440

I am trying to use the function gluTessCallback but I get C2440 error. I have no idea why.
Here is the code:
#define callback void(CALLBACK*)()
template<typename T>
class Tessellation
{
private:
GLUtesselator *pTess;
void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
public:
void Triangulation3D(T* & point, short numOfPoints)
{
pTess = gluNewTess();
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)tessError);
}
};
The error is on gluTessCallback function:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'void (__stdcall *)(void)'
Why do I get this compile error?
The error id C2440 on Visual Studio is a type conversion error.
The problem in your code is that you are trying to pass a class method Tessellation::tessError() as function pointer to gluTessCallback(), which expects a pointer to a global C-style function.
A class method is very different from a free/global function and you cannot pass it as a simple function pointer because it needs an object to go along with it every time, the this pointer.
A solution to your problem would be to declare tessError() as a static method, making it effectively the same as a free/global function scoped inside the class, like so:
template<typename T>
class Tessellation
{
private:
static void CALLBACK tessError(GLenum error)
{
sendErrorMessage((char *)gluErrorString(error), true);
}
...
And pass it to gluTessCallback():
gluTessCallback(pTess, GLU_TESS_ERROR, (callback)&Tessellation<T>::tessError);
The only downside to this approach is that a static tessError() can no longer access class variables. Which doesn't seem like a problem to you, since it is not doing so right now, it appears.

C++ list remove_if compile error

The error message I'm getting for the code below is:
error C2662: 'DamageNumbers::IsAlive' : cannot convert 'this' pointer from 'const DamageNumbers' to 'DamageNumbers &'
1> Conversion loses qualifiers
.
bool CheckDamageNumbersAlive(const DamageNumbers& e)
{
return !e.IsAlive();
}
I want to remove objects from a list when IsAlive() returns false for the objects in that list.
Your method
bool DamageNumbers::IsAlive() {...}
should be const:
bool DamageNumbers::IsAlive() const {...}

C++ Cannot convert from deferred::SafePtr<T> to T*

I have made a test case to show the problems I am running into. Please forgive me my ignorance on the issues of Deferred libraries and pointer casting. The only library included in the deferred.lib.
#include <deferred/deferred.h>
using namespace deferred;
SafePtr<Deferred> recordTime(int time)
{
SafePtr<Deferred> d = createDeferred();
SafePtr<CallbackData> p = new PointerCBD< char>( 0 );
d->execute(p);
return d;
}
int main(int argc, char* argv[])
{
while(1)
{
SafePtr<Deferred> d = recordTime(1000);
d->waitHereForCompletion();
char* c = dynamic_pointer_cast<char>(d->endResult());
}
return 0;
}
When I try and compile in Windows XP with VS2008 I get:
Error C2440: 'initializing' : cannot convert from
'deferred:SafePtr' to 'char *' with
[T=char]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called.
I have tried this command to return a pointer, rather than a SafePtr:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult()).get();
Then I get this error:
Error C2664: 'deferred::intrusive_ptr_release' : cannot convert
parameter 1 from 'char *' to
'deferred:ReferenceCountable *'
I have tried this command:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult().get());
Then I get this error:
Error C2784: 'deferred::SafePtr deferred::dynamic_pointer_cast(const deferred::SafePtr< U > &)' : could not deduce template argument for 'const deferred::SafePtr< U > &' from 'deferred::CallbackData *'
Try this:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult().get());
if you want to get an "unsafe" pointer, or probably this:
SafePtr<ManagerTimings> t= dynamic_pointer_cast<ManagerTimings>(d->endResult());
to get a safe ManagerTimings pointer. I don't know what library you are using, but I suspect that dynamic_pointer_cast can convert a SafePtr to another SafePtr. Or it just converts pointers.
&*d->endResult()
I think from this code endResult is the SafePtr you're having trouble with.

_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.