0

I need to initialize thread with a function that will just call a method of UDPClient class that I pass to the thread

I've got that code:

class UDPClient
{
private:
    boost::asio::io_service& io_service_;
    udp::socket socket_;
    udp::endpoint endpoint_;
public:
    UDPClient(boost::asio::io_service& io_service, std::string host, std::string port) : io_service_(io_service), socket_(io_service, udp::endpoint(udp::v4(), 0)) {
        udp::resolver resolver(io_service_);
        udp::resolver::query query(udp::v4(), host, port);
        udp::resolver::iterator iter = resolver.resolve(query);
        endpoint_ = *iter;
    }
    void send(std::vector<unsigned char> msg) {
        socket_.send_to(boost::asio::buffer(msg, msg.size()), endpoint_);
        std::cout << "sent" << std::endl;
    }
    std::vector<unsigned char> receive() {
        std::vector<unsigned char> receive_data;
        receive_data.resize(65495);
        udp::endpoint sender;
        size_t len = socket_.receive_from(boost::asio::buffer(receive_data), sender);
        receive_data.resize(len);
        return receive_data;
    }

};

void getMsg(UDPClient client) {
    while (true) {
        auto x = client.receive();
        cout << "received "<< std::hex  << x[0] << endl;
    }
}

int main()
{

    boost::asio::io_service io_service;
    UDPClient client(io_service, std::string("localhost"), std::string("5213"));
    std::thread thr1(getMsg, client);

    thr1.join();


}

The following error occurs during compilation

C2661 'std::tuple<void (__cdecl *)(UDPClient),UDPClient>::tuple': no overloaded function takes 2 arguments 
C2672 'invoke': no matching overloaded function found

I tried to pass the class object by pointer, but it got rid of only one of the errors. Code:

void getMsg(UDPClient *client) {
    while (true) {
        auto x = client->receive();
        cout << "received "<< std::hex  << x[0] << endl;
    }
}

int main()
{

    boost::asio::io_service io_service;
    UDPClient client(io_service, std::string("localhost"), std::string("5213"));
    std::thread thr1(getMsg, std::ref(client));

    thr1.join();


}

Errors:

Error C2672 'invoke': no matching overloaded function found
  • 1
    dup? https://stackoverflow.com/q/10673585/4641116 – Eljay Mar 24 '23 at 12:54
  • I don't need to call a class method directly, I need to call a method that calls a class method – disssbalance Mar 24 '23 at 12:57
  • 1
    My screen reader is unable to read your images of the errors. Based on the term "method" I presumed you are calling a *method* (in the object-oriented sense), which is a *member function* in C++ terminology. C++ terminology itself does not use "method". – Eljay Mar 24 '23 at 13:02
  • `std::ref` doesn't produce a pointer, it produces an `std::reference_wrapper`, which can be implicitly converted to a *reference*, not a pointer. And I guess the first example fails because `UDPClient` is non-copyable? At least I'd except something called `socket` to be non-copyable, that operation wouldn't make sense. – Yksisarvinen Mar 24 '23 at 13:08
  • @disssbalance you may try implementing a lambda function to do that. – πάντα ῥεῖ Mar 24 '23 at 13:08
  • @Eljay I am sorry, i need to call a function that calls a class method – disssbalance Mar 24 '23 at 13:10
  • @Yksisarvinen you right, i tried to use & instead of * in function arguments and it worked! – disssbalance Mar 24 '23 at 13:15

0 Answers0