Casablanca/cpprestsdk listener.support接受函数,但不支持方法
Casablanca/cpprestsdk listener.support accepting functions but not methods
要使用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);
当handle_get、handle_post等只是函数时,这工作正常。但是一旦我尝试在控制器类中实现它,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)
我在文档中没有看到任何关于方法不起作用的原因。我也仔细阅读了这些问题,没有看到任何与我的问题相关的内容。
有什么明显的原因为什么听众支持会努力找到方法吗?
我认为您需要绑定方法
listener.support(methods::GET, std::bind(&Controller::handle_get, this, std::placeholders::_1));
http_listener::support
接受类型为const std::function< void(http_request)> &handler
的参数,这意味着您可以传递任何 Callable 类型,因此,为了使用成员函数,您可以使用以下内容:
listener.support(methods::GET, [this](web::http::http_request http_request)
{
// call your member function here or handle your request here
});
或
listener.support(methods::GET, std::bind(&MyClass::my_handler, this, std::placeholders::_1));
第一个示例使用捕获this
的 lambda,第二个示例通过函数创建调用包装器MyClasss::my_handler
相关文章:
- Casablanca/cpprestsdk listener.support接受函数,但不支持方法
- 为什么向量不支持 size 作为成员变量,就像 Java 类中的长度变量一样,而是函数 size()?
- C++11 NVCC 不支持函数 iota()?
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- PGI 不支持 OpenMP 4.5 运行时函数
- createProcessWithLogon返回不支持的函数
- QT快速UI表单不支持函数
- 错误C4430:丢失类型指定词 - 假设INT.注意:C 不支持对我的构造函数的默认设置
- 聚合初始化不支持构造函数访问
- 为什么 g++ 不支持在 Ubuntu 上使用分配器构造函数?
- 我的 cipher() 函数似乎不支持大写字母
- Matlab 编码器不支持的函数
- 函数指针是否不支持C++中的实例类型
- clang 3.5中不支持-finline函数
- Matlab Codegen:不支持匿名函数
- java.lang.IllegalArgumentException:函数参数0处不支持的参数
- 为什么C++"标准"不支持另一个函数中的函数定义?
- 为什么 gcc 不支持裸函数?
- 调用函数时不支持间接强制转换
- 对于不支持RTLD_DEEPBIND的平台,是否有替代 dlopen() 函数中 dlopen() 函数中的标志RTLD_DEEPBIND?