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 {...}
Related
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*>.
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.
I am making a wrapper for a Cpp DLL, but am getting trouble with the following function:
void PyGetUSBDeviceNames(DFUEngine *DFUe, CStringListX &devices)
{
return DFUe->GetUSBDeviceNames(&devices);
}
I get the error:
error C2664: 'DFUEngine::GetUSBDeviceNames' : cannot convert parameter 1 from 'CStringListX *__w64 ' to 'CStringListX &'
The function GetUSBDeviceNames is defined in the h file as:
static int GetUSBDeviceNames(CStringListX &devices);
how can I get around this one?
Thanks alot
Remove the unneeded &:
return DFUe->GetUSBDeviceNames(devices);
maybe you would know, I get an error:
error C2440: 'initializing' : cannot convert from 'int'
Conversion from integral type to pointer type requires reinterpret_cast
It goes to ' file in MS VS 2010 folder:
template<class _Other1,
class _Other2>
_Pair_base(_Other1&& _Val1, _Other2&& _Val2)
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
I was looking for different solutions but could not find a correct one.
The error says
'initializing' : cannot convert from 'int' to 'EnterFunctor *'
The only part of your code you share is
functors.push_back(make_pair(sessionStartFunc,
pair<EnterFunctor*, ExitFunctor*>(NULL,sessionStartExit)));
If NULL is #defined as 0 this gives you an int but you promised a pair of pointers, so as the next line of the error says you can use a cast to make NULL the right type of pointer.
I know that the title is not very clear but I didn't know how to write it down in one sentence. So the problem is that I want something like this:
void(typeof(this)::*function)(int,int);
I know that is not going to work but I was wandering whether a solution exists for this problem in c++ or not?
Update:
class MainPage
{
public:
MainPage()
{
void (std::remove_reference<decltype(*this)>::*callback)(int, int) = &MainPage::myFunction;
((*this).*callback)(nullptr,nullptr);
}
~MainPage()
{
}
void myFunction(int a, int b)
{
}
}
Errors:
error C2440: 'newline' : cannot convert from 'MainPage *' to 'std::remove_reference<_Ty> *'
error C2647: '.*' : cannot dereference a 'void (__thiscall std::remove_reference<_Ty>::* )(int,int)' on a 'MainPage'
Yes, use decltype:
void (std::remove_reference<decltype(*this)>::type::*function)(int, int);