c ++没有用于从derived-class调用基类方法的匹配函数

时间:2013-03-30 16:00:14

标签: c++ derived-class base-class member-function-pointers

我有点困惑,因为gcc在消息中删除了错误

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

所以我做了一个错误的函数调用,因为它似乎是。这是我从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> > >&))

我得到了一个由 Services :: UserService :: UserService 派生的基类 Lib :: service :: Service 。 我做了另一个基类 Lib :: request :: Request ,它是由 Services :: UserService :: Request :: LoginRequest

派生的

基类Lib :: service :: Service实现了一个名为“registerMethod”的方法,它接受一个字符串和一个函数指针。

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

...

void registerMethod(string MethodName, FuncPtr Func);

所以,格式化gcc然后有点给我这个:

请求是:

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> > 
        >&
    )
)

并且gcc说,a(和唯一一个 - 我想要使用的那个)候选人是:

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> > 
        >&
    )
)

因此,Services :: UserService :: UserService派生自Lib :: service :: Service 和Serices :: UserService :: Request :: LoginRequest派生自Lib :: request :: Request我认为这是有效的,因为我定义了匹配基类的函数,因此也可以将它们与派生类一起使用。

我在哪里错了?如果这有帮助,这里有一些代码; - )

到目前为止感谢!

致以最诚挚的问候,

塞巴斯蒂安


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 个答案:

答案 0 :(得分:1)

您似乎忘了在声明方法体中放置void。你有以下代码,在类中声明方法:

void registerMethod(string MethodName, FuncPtr Func);

然后你必须在void声明身体。

void Services::UserService::UserService::registerMethod( ...
^^^^

答案 1 :(得分:0)

所以我终于自己解决了,感谢每一位贡献者。不,谢谢那些没有阅读我的Q并认为他们必须判断我的努力的人。

但是,如果其他人遇到此问题:

就像我在最初的帖子中写的那样,电话是错的。为了工作,需要对目标函数指针进行reinterpret_cast。这适用于派生类。

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