In Delphi, the Timage component can play an animated GIF image:
(Image1.Picture.Graphic as TGIFImage).Animate := True;
I convert this code to C++ equivalent:
((TGIFImage)Image1->Picture->Graphic)->Animate = true;
But I get an error:
[bcc32c Error] Unit1.cpp(60): no matching conversion for C-style cast from 'Vcl::Graphics::TGraphic *' to 'Vcl::Imaging::Gifimg::TGIFImage'
Vcl.Imaging.GIFImg.hpp(937): candidate constructor not viable: requires 0 arguments, but 1 was provided
What's the problem?
You are missing a * in your type-cast:
((TGIFImage*)Image1->Picture->Graphic)->Animate = true;
^
And then, consider using a C++-style cast instead of a C-style cast:
static_cast<TGIFImage*>(Image1->Picture->Graphic)->Animate = true;
Related
When I am calling SetParams function from the below function, it is throwing an error "cannot convert argument 1 from 'std::wstring' to 'wchar_t *'"
Can anyone please help me on this?
int main()
{
IVerify* pReader = new BCReader();
std::wstring oemPathKey;
pReader->SetParams(oemPathKey, L"read");
delete pReader;
return 0;
}
void BCReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
m_wszParamType = wszParamType;
m_wszParams = wszParams;
}
The member variables are declared like as shown below:
class IVerify
{
private:
wchar_t* m_wszParams;
wchar_t* m_wszParamType;
};
There are two parts of the right answer:
1. You need to use pReader->SetParams(oemPathKey.c_str(), L"read");
2. Your approach is not safe, you trying to keep pointer to string in the class members.
But if the original string will go out of scope then you will receive Access Vioalation, if you are lucky :). So in SetParams you need to copy source string to the class members uisng for example wscpy (I recommend something like wscpy_s), also you need correctly handle allocation/deallocation for string copy.
I am porting some code from VS to mingw C++ . One of the statements in my code is
CFactoryTemplate g_Templates[1] = {
{&CLSID_SystemClock, CSystemClock::CreateInstance}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
I am getting the following error on the first statement
error: cannot convert 'const GUID* {aka const _GUID*}' to 'const
WCHAR* {aka const wchar_t*}' in initialization
I am completely puzzled by this. I did a little investigation and noticed that
CFactoryTemplate is a class in combase.h . Also my project is UNICODE enabled if that matters. Any suggestions on how to resolve this issue ?
Your code - you say you are porting is wrong, you need to provide different parameters to CFactoryTemplate, the compiler error proves that. Here you will find some sample code to init such array of instances of this class (you dont need to fill all fields):
// list of class ids and creator functions for class factory
CFactoryTemplate g_Templates[2]=
{ { L"Gargle filter" // CFactoryTemplate.m_name
, &CLSID_Gargle // CFactoryTemplate.m_ClsID
, CGargle::CreateInstance // CFactoryTemplate.m_lpfnNew
, NULL // CFactoryTemplate.m_lpfnInit
, &sudGargle // CFactoryTemplate.m_pAMovieSetup_Filter
}
, { L"Gargle filter property page"
, &CLSID_GargProp
, CGargleProperties::CreateInstance
}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
from https://msdn.microsoft.com/en-us/library/aa451506.aspx
also Hans Passant has given you other example
My code looks like this:
void C::addB(std::atomic<B>& b)
{
B* b2 = b.load();
B newValue = B();
bool result = b.compare_exchange_weak(b2, newValue, std::memory_order_relaxed, std::memory_order_release);
}
and the compiler keeps complaining that the signature does not match a three-member overloaded form of compare_exchaneg_weak:
note: candidate expects 3 arguments, 4 provided
Your code gives me rather more error messages than the fragment you've posted. The most relevant are
error: cannot convert ‘B’ to ‘B*’ in initialisation
note: no known conversion for argument 1 from ‘B*’ to ‘B&’
indicating that you're declaring a pointer when you want an object:
B b2 = b.load();
http://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange
Refer the prototypes from above URL and pass parameters accordingly.
In cocos2d-x, the following piece of code is supposed to run the callback function after a delay. What do I need to do to fix the error?
bool LoadingLevelScreen::initialise() {
// set up the time delay
CCDelayTime *delayAction = CCDelayTime::actionWithDuration(0.5f);
// perform the selector call
CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(
this, callfunc_selector( LoadingLevelScreen::menuCallbackStart ) );
// run the action
this->runAction( CCSequence::actions(
delayAction, callSelectorAction, NULL ) );
}
void LoadingLevelScreen::menuCallbackStart(CCObject * pSender)
{
}
Compiler Error:
error C2440: 'type cast' :
cannot convert from 'void (__thiscall LoadingLevelScreen::* )(cocos2d::CCObject *)'
to 'cocos2d::SEL_CallFunc'
Pointers to members have different representations; cannot cast between them
Either remove the CCObject* parameter in menuCallbackStart() method (because CCCallFunc::actionWithTarget() expects a method with no arguments), or change CCCallFunc to CCCallFuncO which expects a method with a CCObject* as argument, like so:
CCCallFuncO * callSelectorAction =
CCCallFuncO::create(this, &LoadingLevelScreen::menuCallbackStart, myObject);
where myObject is a CCObject * that will be passed to your method as the argument.
Note that callfunc_selector() is just a macro that typecasts your method to SEL_CallFunc:
#define callfunc_selector(MYSELECTOR) (SEL_CallFunc)(& (MYSELECTOR))
BTW ::actionWithTarget() is being deprecated, so use ::create() instead.
void LoadingLevelScreen::menuCallbackStart(CCObject * pSender)
{
}
should be
void LoadingLevelScreen::menuCallbackStart()
{
}
callfunc_selector is different with menu_selector, you don't need the CCObject* to pass in as a variable
if you do need to pass argument, please use callFuncND
this->runAction(Sequence::create(CallFunc::create(std::bind(&CNm::MNm, this)),NULL));
this->runAction(Sequence::create(CallFunc::create(std::bind(&ClassName::MethodName, this)),NULL));
I just told Xcode to compile everything as Objective-C++ and now I have errors from casting.
void audioRouteChangeListenerCallback (
void *aInUserData,
AudioSessionPropertyID aInPropertyID,
UInt32 aInPropertyValueSize,
const void *aInPropertyValue
) {
// Ensure that this callback was invoked because of an audio route change
if (aInPropertyID != kAudioSessionProperty_AudioRouteChange) return;
// This callback, being outside the implementation block, needs a reference to the MixerHostAudio
// object, which it receives in the inUserData parameter. You provide this reference when
// registering this callback (see the call to AudioSessionAddPropertyListener).
TJUSimpleSequencer *lAudioObject = (TJUSimpleSequencer *) aInUserData;
// if application sound is not playing, there's nothing to do, so return.
if (NO == lAudioObject.isPlaying) {
NSLog (#"Audio route change while application audio is stopped.");
return;
} else {
// Determine the specific type of audio route change that occurred.
CFDictionaryRef routeChangeDictionary = aInPropertyValue; // !!! invalid conversion from 'const void*' to 'const __CFDictionary*'
CFNumberRef routeChangeReasonRef =
CFDictionaryGetValue (
routeChangeDictionary,
CFSTR (kAudioSession_AudioRouteChangeKey_Reason)
); // !!! invalid conversion from 'const void*' to 'const __CFNumber*'
When i try to use static_cast<CFDictionaryRef>(aInPropertyValue), I get nothing. As though (which is probably true) I am not using it correctly.
Use regular C cast?
CFDictionaryRef routeChangeDictionary = ( CFDictionaryRef )aInPropertyValue;
Edit 0:
How about this for that function call (if you're sure it's a number :)
CFNumberRef routeChangeReasonRef =
( CFNumberRef )CFDictionaryGetValue ( ...
Edit 1:
Take a look at The Definitive C++ Book Guide and List then.