我必须使用尚未编写的部分代码来为学校做TP。该函数的原型如下:
using Callback =
std::function< bool(std::string const& request, std::string& response) >;
TCPServer::TCPServer(Callback const& callback) :
callback_(callback) {
// signal(SIGPIPE, SIG_IGN); // ignore nasty SIGPIPEs
}
我想使用的函数是名为的类的成员函数whole
。我正在使用此代码:
// cree l'objet qui gère les données
// create the object that manages the data
shared_ptr<Whole> whole(new Whole());
whole->createPhoto("Photo1", "undefined/paths", 0, 0);
// cree le TCPServer
shared_ptr<TCPServer> server = new TCPServer(&whole::processRequest)
但是由于某种原因,这似乎无法编译。我收到错误消息:“错误:“整个”不是类,名称空间或枚举shared_ptr服务器=新的TCPServer(&whole :: processRequest);”
有人能帮我吗 ?我不知道我应该使用哪种语法。
这是我的老师为实现类TCPserver给出的示例:
new TCPServer( [&](std::string const& request, std::string& response) {
// the request sent by the client to the server
std::cout << "request: " << request << std::endl;
// the response that the server sends back to the client
response = "RECEIVED: " + request;
// return false would close the connection with the client
return true;
});
我假设将Whole :: processRequest声明为:
bool Whole::proessRequest(string const&, string&);
因此,与此相反,在没有Whole
要传递的实例的情况下,只是一个指向没有Whole
关联实例的函数的指针:
shared_ptr<TCPServer> server = new TCPServer(&whole::processRequest)
改为这样做。创建lambda包装器,以捕获您的whole
shared_ptr实例并按您期望的方式调用该方法。
auto fn = [whole](std::string const& request, std::string& response) -> bool {
return whole->processRequest(request, response);
};
shared_ptr<TCPServer> server = new TCPServer(fn);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句