Tutorial of LazyFoo: 45 - timer callbacks does not compile in Xcode

Hi guys, I was trying to run the following tutorial in Xcode 8.0
but there is an error: no matching function for call to 'SDL_AddTimer’
How can I please fix that?

Many thanks!

http://lazyfoo.net/tutorials/SDL/45_timer_callbacks/index.php

 Uint32 callback( Uint32 interval, void* param )
    {
        //Print callback message
        cout << "Callback called back with message: \n" << (char*)param;
        
        return 0;
    }

    int main( int argc, char* args[] )
    {
        //Start up SDL and create window
        if( !init() )
        {
            printf( "Failed to initialize!\n" );
        }
        else
        {
            //Load media
            if( !loadMedia() )
            {
                printf( "Failed to load media!\n" );
            }
            else
            {	
                //Main loop flag
                bool quit = false;
                
                //Event handler
                SDL_Event e;
                
                //Set callback
                SDL_TimerID timerID = SDL_AddTimer( 3 * 1000, callback, "3 seconds waited!"  ); /*this line does not compile! */
                
                //While application is running
                while( !quit )
                {
                    //Handle events on queue
                    while( SDL_PollEvent( &e ) != 0 )
                    {
                        //User requests quit
                        if( e.type == SDL_QUIT )
                        {
                            quit = true;
                        }
                    }
                    
                    //Clear screen
                    SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
                    SDL_RenderClear( gRenderer );
                    
                    //Render splash
                    gSplashTexture.render( 0, 0 );
                    
                    //Update screen
                    SDL_RenderPresent( gRenderer );
                }
                
                //Remove timer in case the call back was not called
                SDL_RemoveTimer( timerID );
            }
        }
        
        //Free resources and close SDL
        close();
        
        return 0;
    }

This is weird, the call to SDL_AddTimer() looks correct and the callback
also has the correct signature.
When/where from do you get that error? When compiling (from clang) or is
it just some Xcode Editor hint? You could safely ignore the latter…
If it’s a compiler error, could you post more of it (I guess you’d get
several lines of information on the error then)?

Cheers,
Daniel

SDL_TimerID timerID = SDL_AddTimer( 3 * 1000, callback, “3 seconds waited!”);

Hi there the full error output is the following:


No matching function for call to ‘SDL_AddTimer’

extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
SDL_TimerCallback callback,
void *param);

/Library/Frameworks/SDL2.framework/Headers/SDL_timer.h:93:37: Candidate function not viable: no known conversion from ‘const char [18]’ to ‘void *’ for 3rd argument

Any idea how to fix it?

Many thanks in advance.

Ah, it’s about the const (string literals like “3 seconds waited” are
implicitly const)!
param is a non-const pointer, so while converting from char* or char[42]
to void* would just work, it doesn’t work for const char* or const
char[42] - you need to add an explicit cast to make it work, like:

SDL_AddTimer( 3 * 1000, callback, (void*)“3 seconds waited!” );

Cheers,
Daniel

1 Like

Perfect, now it works! Many thanks for your help and explanation! :slight_smile:

2 Likes

Thank you for your correction. Appreciate your explanation!
I had tried
SDL_AddTimer (3 * 1000, callback, (void *) (“3 seconds waited!”));
which didn’t work. :confused:
Care to explain why?