I got a little confused as gcc dropped an error with the message

error: no matching function for call to ...
note: candidates are ...

So I did a wrong function call as it seems to be. Here is what I really got from gcc:

src/Services/UserService/UserService.cpp:17: error: no matching function for call to ‘Services::UserService::UserService::registerMethod(const char [6], Services::UserService::Request::LoginRequest* (Services::UserService::UserService::*)(std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param> > >&))’
src/Services/UserService/../../Lib/Service/Service.hpp:47: note: candidates are: void Lib::service::Service::registerMethod(std::string, Lib::request::Request* (Lib::service::Service::*)(std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param> > >&))

I got a base class Lib::service::Service that is derived by Services::UserService::UserService. And I did another base class Lib::request::Request which is derived by Services::UserService::Request::LoginRequest

The base class Lib::service::Service implements a method called "registerMethod", which takes a string and a function pointer.

typedef Lib::request::Request* (Lib::service::Service::*FuncPtr)(map<string, Param>&);

...

void registerMethod(string MethodName, FuncPtr Func);

So, formatting the gcc outbut a bit gives me this:

Requested is:

Services::UserService::UserService::registerMethod(
    const char [6], 

    Services::UserService::Request::LoginRequest* (
        Services::UserService::UserService::*
    )(
        std::map<
            std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
            Lib::request::Param, 
            std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
            std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param> > 
        >&
    )
)

And gcc says, that a (and the only one - and that one I want to be used) candidate is:

void 
Lib::service::Service::registerMethod(
    std::string, 

    Lib::request::Request* (
        Lib::service::Service::*
    )(
        std::map<
            std::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
            Lib::request::Param, 
            std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, 
            std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Lib::request::Param> > 
        >&
    )
)

So, as Services::UserService::UserService is derived from Lib::service::Service and Serices::UserService::Request::LoginRequest is derived from Lib::request::Request I thought this is working as I defined the function to match the base classes and therefore could use them with the derived classes too.

Where am I wrong here? If this helps, here is some more code ;-)

Thanks so far!

Best regards,

Sebastian


namespace Lib {
    namespace service {
        class Service;
    }
}

namespace Lib {
    namespace request {

        class Request {
        public:
            Request(Lib::service::Service *Owner);
            virtual ~Request();

            virtual void Execute() = 0;

            void join(Lib::Session::Session *session);

        protected:
            Lib::service::Service *module;
            Lib::Session::Session *session;
            map<string, Param> params;

        private:
        };
    }
}

typedef Lib::request::Request* (Lib::service::Service::*FuncPtr)(map<string, Param>&);

namespace Lib {
    namespace service {

        class Service {
        public:
            const string Name;

            Service();
            virtual ~Service();

            Request* Call(string MethodName, map<string, Param> &Params);

        protected:
            void registerMethod(string MethodName, FuncPtr Func);

        private:
            map<string, FuncPtr> methods;
        };
    }
}

-

namespace Lib
{
    namespace service
    {

        Service::Service()
        {
        }

        Service::~Service()
        {
        }

        void Service::registerMethod(string MethodName, FuncPtr Func)
        {
            this->methods.insert(pair<string, FuncPtr>(MethodName, Func));
        }

        Request* Service::Call(string MethodName, map<string, Param> &Params)
        {
            FuncPtr Func;

            Func = this->methods[MethodName];
            Request *req = (*this.*Func)( Params );

            return req;
        }
    }
}

-

namespace Services {
    namespace UserService {

        class UserService : public Lib::service::Service {
        public:
            const string Name;

            UserService();
            virtual ~UserService();

            LoginRequest* Login(map<string, Param> &params);
            LogoutRequest* Logout(map<string, Param> &params);
        private:

        };
    }
}

-

namespace Services
{
    namespace UserService
    {

        UserService::UserService() : Name("UserModule")
        {
            this->registerMethod("Login", &UserService::Login);
            this->registerMethod("Logout", &UserService::Logout);
        }

        UserService::~UserService()
        {
        }

        LoginRequest* UserService::Login(map<string, Param> &params)
        {
            LoginRequest *request = new LoginRequest(this);

            //...

            return request;
        }

        LogoutRequest* UserService::Logout(map<string, Param> &params)
        {
            LogoutRequest *request = new LogoutRequest(this);

            //...

            return request;
        }

    }
}
有帮助吗?

解决方案 2

So I finally solved it by myself, thanks for every contributor. No thanks to those people who did not read my Q and thought they have to judge about my effort.

However, if someone else run into this:

It was quite as I wrote in my initial post, the call was wrong. To work, there is a need to do a reinterpret_cast to the target function pointer. This works for the derived classes.

this->registerMethod("Login", reinterpret_cast<FuncPtr>(&UserService::Login));

其他提示

It seems you forget to put a void in declaring body of the method. You have below code, declaring the method in class:

void registerMethod(string MethodName, FuncPtr Func);

Then you must put void in declaring the body.

void Services::UserService::UserService::registerMethod( ...
^^^^
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top