C++ 作为参数传递对象的方法
我必须为我的学校做一个TP,使用我没有编写的部分代码。 该功能的原型如下所示:C++ 作为参数传递对象的方法,c++,C++,我必须为我的学校做一个TP,使用我没有编写的部分代码。 该功能的原型如下所示: using Callback = std::function< bool(std::string const& request, std::string& response) >; TCPServer::TCPServer(Callback const& callback) : callback_(callback) { // signal(SIGPIPE, SIG_I
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
}
但出于某种原因,这似乎无法编译。我得到一个错误:
错误:“整”不是类、命名空间或枚举
shared_ptr server=new 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;
});
newtcpserver([&](std::string const&request,std::string&response){
//客户端发送到服务器的请求
std::cout我假定整个::processRequest声明为:
bool Whole::proessRequest(string const&, string&);
因此,与此相反,如果没有传递Whole
的实例,则只传递一个指向没有与之关联的Whole
实例的函数的指针:
shared_ptr<TCPServer> server = new TCPServer(&whole::processRequest)
发布完整的编译错误消息这不是严格要求的,但我建议也将注释翻译成英文。这对读者会有帮助。您是否包含声明整篇的头文件?下面是完整的错误消息://cree l'objet qui gère les données shared_ptr整篇(新整篇());整篇->createPhoto(“Photo1,“undefined/paths”,0,0);//cree le TCPServer shared_ptr server=new TCPServer(&whole::processRequest)”。对于注释,它们在本例中非常不相关,这有助于理解全局问题,但我只需要获得函数调用的正确语法。
shared_ptr<TCPServer> server = new TCPServer(&whole::processRequest)
auto fn = [whole](std::string const& request, std::string& response) -> bool {
return whole->processRequest(request, response);
};
shared_ptr<TCPServer> server = new TCPServer(fn);