Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:49:23+00:00 2026-05-13T16:49:23+00:00

I have class which handles packages: typedef void (*FCPackageHandlerFunction)(FCPackage*); class FCPackageHandlers{ … void registerHandler(FCPackage::Type

  • 0

I have class which handles packages:

typedef void (*FCPackageHandlerFunction)(FCPackage*);
class FCPackageHandlers{
    ...
    void registerHandler(FCPackage::Type type, FCPackageHandlerFunction handler);
    void handle(FCPackage* package);
    ...
    QHash<FCPackage::Type, FCPackageHandlerFunction> _handlers;
};

Then I have a server class who receive packages. Now I want to register a function who handles the packages. But this function must have a instance of the server for other variables.

So i try this:

struct FCLoginHandler{
    FCServer* server;

    FCLoginHandler(FCServer* server){
        this->server = server;
    }

    void operator()(FCPackage* package){
        std::cout << "Received package: " << package->toString().data() << "\n";
    }
};

...

FCServer::FCServer(){
    _handlers.registerHandle(FCPackage::Login, FCLoginHandler(this));
}

But then I get this error:

error: no matching function for call to 'FCPackageHandlers::registerHandler(FCPackage::Type, FCLoginHandler)'
note: candidates are: void FCPackageHandlers::registerHandler(FCPackage::Type, void (*)(FCPackage*))

Does anybody know the right solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:49:24+00:00Added an answer on May 13, 2026 at 4:49 pm

    You are trying to store a function object in a function pointer, and that’s not possible. You should store a std::tr1::function instead:

    #include <functional>
    
    typedef std::tr1::function<void(FCPackage*)> FCPackageHandlerFunction;
    class FCPackageHandlers{
        ...
        void registerHandler(FCPackage::Type type, FCPackageHandlerFunction handler);
        void handle(FCPackage* package);
        ...
        QHash<FCPackage::Type, FCPackageHandlerFunction> _handlers;
    };
    

    There’s a similar function class in Boost in case you don’t have access to std::tr1 yet.

    Also, consider using boost::bind and a regular function to avoid the boilerplate of having to create your own function objects such as FCLoginHandler:

    void handle_FC_login(FCServer* server, FCPackage* package)
    {
        std::cout << "Received package: " << package->toString().data() << "\n";
        // You can use server if you need it
    }
    
    
    FCServer::FCServer()
    {
        _handlers.registerHandle(FCPackage::Login, 
                                 std::tr1::bind(&handle_FC_login, this, _1)); 
    }
    

    std::tr1::bind is also available in Boost, and if you don’t have access to that either you can always use std::bind2nd.

    EDIT: Since you can’t modify the type of FCPackageHandlerFunction, your best shot might be to add another hash that stores data associated to each function pointer:

    typedef void (*FCPackageHandlerFunction)(FCPackage*);
    class FCPackageHandlers{
        ...
        void registerHandler(FCPackage::Type type, FCPackageHandlerFunction handler,
                             FCServer * func_data);
        void handle(FCPackage* package);
        ...
        QHash<FCPackage::Type, FCPackageHandlerFunction> _handlers;
        QHash<FCPackageHandlerFunction, FCServer*> _handler_func_data;
    };
    
    // The server will be passed by the package handler which will 
    // extract it from the _handler_func_data hash
    void handle_FC_login(FCServer* server, FCPackage* package)
    {
        std::cout << "Received package: " << package->toString().data() << "\n";
    }
    
    FCServer::FCServer(){
        _handlers.registerHandle(FCPackage::Login, &handle_FC_login, this );
    }
    

    Presumably this is how you’ll have to implement FCPackageHandlers::handle:

    void FCPackageHandlers::handle(FCPackage * package)
    {
        // Query function
        FCPackageHandlerFunction func = _handlers[package->GetType()];
    
        // Query associated data
        FCServer * server = _handler_func_data[func];
    
        // Call handler
        func(server, package);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The simplest way to achieve this? Just have another NSTimer… May 15, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer You don't have to have a reference to Invoice in… May 15, 2026 at 6:17 pm
  • Editorial Team
    Editorial Team added an answer Well, it turns out that debugger continues execution of code… May 15, 2026 at 6:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.