[英]Casablanca/cpprestsdk listener.support accepting functions but not methods
To support HTTP methods using Casablanca/cpprestsdk you can use code like this要使用 Casablanca/cpprestsdk 支持 HTTP 方法,您可以使用这样的代码
http_listener listener(U("http://localhost:10000/restdemo"));
listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);
listener.support(methods::PUT, handle_put);
listener.support(methods::DEL, handle_del);
This works fine when handle_get, handle_post, etc. are simply functions.当 handle_get、handle_post 等只是简单的函数时,这可以正常工作。 But as soon as I try to implement this inside a Controller class with handle_get, handle_post, etc. being methods I get errors like:
但是,一旦我尝试在 Controller class 中使用 handle_get、handle_post 等方法实现这一点,我就会遇到如下错误:
error: no matching function for call to ‘Controller::handle_get()’
error: invalid use of non-static member function ‘void Controller::handle_get(web::http::http_request)
I don't see anything in the documentation for why methods wouldn't work.我在文档中没有看到任何方法为什么不起作用的任何内容。 I also perused through the issues and didn't see anything relating to my problem.
我还仔细阅读了这些问题,没有看到任何与我的问题有关的内容。
Is there any obvious reason why listener.support would struggle to find the methods? listener.support 很难找到方法有什么明显的原因吗?
I think you need to bind the methods我认为您需要绑定方法
listener.support(methods::GET, std::bind(&Controller::handle_get, this, std::placeholders::_1));
http_listener::support
accepts a parameter of type const std::function< void(http_request)> &handler
, that means that you can pass any Callable type, so, in order to use a member function, you can use the following: http_listener::support
接受const std::function< void(http_request)> &handler
类型的参数,这意味着您可以传递任何Callable类型,因此,为了使用成员 function,您可以使用以下内容:
listener.support(methods::GET, [this](web::http::http_request http_request)
{
// call your member function here or handle your request here
});
or或者
listener.support(methods::GET, std::bind(&MyClass::my_handler, this, std::placeholders::_1));
The first example uses a lambda with this
captured, the second creates a call wrapper over function MyClasss::my_handler
第一个示例使用 lambda 并捕获
this
内容,第二个示例在 function MyClasss::my_handler
上创建调用包装器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.