Answer the question
In order to leave comments, you need to log in
How to bind classmember handler for boost::asio?
When compiling, an error occurs if I use tcp::socket in the session argument, if I use int or shred_ptr, everything compiles. How to bind correctly?
#include <thread>
#include <memory>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
using namespace std;
class Server
{
private:
boost::asio::io_service io_service;
public:
void session(tcp::socket sock)
{
cout << "New connection..." << endl;
}
void start(unsigned port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
cout << "Server started..." << endl;
for (;;) {
tcp::socket sock(io_service);
a.accept(sock);
thread(boost::bind(&Server::session, this, move(sock))).detach(); //выдаёт ошибку чёто типа using deleted function
}
}
};
int main()
{
auto server = make_shared<Server>();
server->start(5000);
return 0;
}
Answer the question
In order to leave comments, you need to log in
99% that the removed function is a copy constructor.
By the meaning of the socket object, you can guess that it is not worth copying (because one way or another it is a wrapper over a system resource). Since it's not worth copying, use a socket pointer instead of the socket itself. For example, shared_ptr, as advised here: stackoverflow.com/questions/5425666/passing-around... .
In general, get used to the fact that complex objects are passed by value quite rarely (almost never), because passing by value contradicts the essence of these objects - to exist strictly in the quantity in which the programmer created them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question