Close
Close

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

  •   
    #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;
    }
    

     



  •   

    //warning:  Mind compiled code, intended to illustrate the mechanism   
    #include<iostream>
    #include <map>
    using namespace std;


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

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

    bool finished = false;

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

    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 =callback_map[event];
                if (function)   
                {
                   (*function)();
                }
                else
                {
                   std::cout << "No callback found ";
                }
            }
        }
    }

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

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

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

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

        EventProcessor();
        return 0;
    }


    • I hope it will work try it
      - onteru.s

Ask Yours
Post Yours
Write your answer