logo codesdope

Help needed in solving the compilation error .

   Nihar

Hi I Am gettign this error with the following code.

E:\Cpp>g++ callback.cpp
callback.cpp: In function 'void EventProcessor()':
callback.cpp:33:34: error: cannot convert 'const std::pair<const int, void (*)()>' to 'Callback {aka void (*)()}' in initialization
             Callback function = *it;
callback.cpp: In function 'int main()':
callback.cpp:63:28: error: 'RegisterCallBack' was not declared in this scope
     RegisterCallBack(1, Cat)

Weird part is am not getting any clue how to fix it,moreover am trying to understand the code by compiling it .

Here is the code

 

#include<iostream>
#include <map>


typedef void (*Callback)();
std::map<int, Callback>  callback_map;

void RegisterCallback(int,std::map<int, Callback>);

void RegisterCallback(int event, Callback function)
{
    callback_map[event] = function;
}

bool finished = false;

int GetNextEvent()
{
    static int i = 0;
    ++i;
    if (i == 5) finished = false;
}

void EventProcessor()
{
    int event;
    while (!finished)
    {
        event = GetNextEvent();
        std::map<int, Callback>::const_iterator it = callback_map.find(event);
        if (it != callback_map.end())    // if a callback is registered for event
        {
            Callback function = *it;
            if (function)   
            {
                (*function)();
            }
            else
            {
               std::cout << "No callback found\n";
            }
        }
    }
}

void Cat()
{
   std::cout << "Cat\n";
}

void Dog()
{
    std::cout << "Dog\n";
}

void Bird()
{
    std::cout << "Bird\n";
}

int main()
{
    RegisterCallBack(1, Cat);
    RegisterCallback(2, Dog);
    RegisterCallback(3, Cat);
    RegisterCallback(4, Bird);
    RegisterCallback(5, Cat);

    EventProcessor(); 
    return 0;
}

 

Any help is highly appreciated.


Answers

  •    Amit Kumar , I.I.T. Patna
    #include<iostream>
    #include <map>
    
    
    typedef void (*Callback)();
    std::map<int, Callback>  callback_map;
    
    void RegisterCallback(int,std::map<int, Callback>);
    
    void RegisterCallback(int event, Callback function)
    {
        callback_map[event] = function;
    }
    
    bool finished = false;
    
    //Make sure to return an integer
    int GetNextEvent()
    {
        static int i = 0;
        ++i;
        if (i == 5) finished = false;
    }
    
    void EventProcessor()
    {
        int event;
        while (!finished)
        {
            event = GetNextEvent();
            std::map<int, Callback>::const_iterator it = callback_map.find(event);
            //I am not sure what are you doing here.
            if (it != callback_map.end())    // if a callback is registered for event
            {
                Callback function = *it;
                if (function)   
                {
                    (*function)();
                }
                else
                {
                   std::cout << "No callback found\n";
                }
            }
        }
    }
    
    void Cat()
    {
       std::cout << "Cat\n";
    }
    
    void Dog()
    {
        std::cout << "Dog\n";
    }
    
    void Bird()
    {
        std::cout << "Bird\n";
    }
    
    int main()
    {
        //should be RegisterCallback (Back)
        RegisterCallback(1, Cat);
        RegisterCallback(2, Dog);
        RegisterCallback(3, Cat);
        RegisterCallback(4, Bird);
        RegisterCallback(5, Cat);
    
        EventProcessor(); 
        return 0;
    }
    

     



Share what you know
Write your answer
Close

Welcome.please sign up.

Close

Welcome.please login.