我有点困惑,因为 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> ¶ms);
LogoutRequest* Logout(map<string, Param> ¶ms);
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> ¶ms)
{
LoginRequest *request = new LoginRequest(this);
//...
return request;
}
LogoutRequest* UserService::Logout(map<string, Param> ¶ms)
{
LogoutRequest *request = new LogoutRequest(this);
//...
return request;
}
}
}