Error with C ++ thead

This is the main file for a little socket demo, I want to use a stream to get the server response, but when I try to create a new stream:

error message:

 error: no matching constructor for initialization of 'std::thread'

      

code:

#include <iostream>
#include <thread>
#include "client_socket.h"
#include "socket_exception.h"

void receive(ClientSocket client)
{
     std::string reply;
     while (true) {
        client >> reply;
        std::cout << "We received this response from the server:" << std::endl;
        std::cout << "\"" << reply << "\"" << std::endl;
    }
}

int main(int argc, const char* argv[])
{
    try {
        ClientSocket client("127.0.0.1", 30000);
        std::string sendBuf;

        std::thread receiver(receive, client);
        receiver.join();

        while (true) {
            std::cout << "Enter string send to server:" << std::endl;
            std::cin >> sendBuf;
            if (sendBuf.compare("quit") == 0) {
                break;
            }
            try {
                client << sendBuf;
            } catch (SocketException&) {}
        }
    } catch (SocketException& e) {
        std::cout << "Exception was caught:" << e.description() << std::endl;
    }
    return 0;
}

      

Is there something wrong with using thread? thank

+3


source to share


1 answer


As far as I can see, you have a valid thread constructor call, as there is a templated constructor for 1..N arbitrary arguments. Invalid arguments will result in compilation errors within this constructor, not the error message you receive. Other possible errors, eg. not using recoginze receive

as a reference to a function you defined earlier will also result in various error messages. It looks like a templated multi-argument constructor is missing from your implementation std::thread

.



IIRC a few years ago there were early implementations std::thread

in which only one argument was accepted, i.e. you would need to provide a null callable object or function for example. calling std::bind(receive, client)

. In this case, you will need to update your compiler to a more modern version.

+1


source







All Articles