-1

我有点困惑,因为 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。我做了另一个由Services::UserService::Request::LoginRequest派生的基类Lib::request::Request

基类 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 说,一个(也是唯一一个 - 也是我想要使用的那个)候选人是:

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

    }
}
4

2 回答 2

1

似乎您忘记void在方法的声明主体中添加一个。您有以下代码,在类中声明方法:

void registerMethod(string MethodName, FuncPtr Func);

然后你必须void声明身体。

void Services::UserService::UserService::registerMethod( ...
^^^^
于 2013-03-30T16:02:35.650 回答
1

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

但是,如果其他人遇到这种情况:

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));
于 2013-03-31T00:18:37.357 回答