-1

The following code piece does not compile for me:

#include <iostream>
#include <boost/thread.hpp>


int main(int argc, char* argv[])
{
  boost::thread thread(
                []() {
                    std::cout<<"hello";
                }
            );
}

With the error :

no matching function for call to ‘boost::thread::thread(main(int, char**)::<lambda()>)’

I feel like I am making a very stupid mistake here, but it has been sometime, and i still fail to find it.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • You probably want to capture `io_service`. – Barry Jun 19 '15 at 18:48
  • I copied the wrong version. Edited the question. I was capturing it. – Özüm Safaoğlu Jun 19 '15 at 18:49
  • Also even if I dont use io_service, and give a dummy cout, it still gives the same error. – Özüm Safaoğlu Jun 19 '15 at 18:50
  • 3
    How about you provide a [Minimal, Complete, Verifiable Example](http://www.stackoverflow.com/help/mcve)? – Barry Jun 19 '15 at 18:51
  • 1
    why don't you capture by reference? – CinchBlue Jun 19 '15 at 18:52
  • I have also tried capturing by reference. But how is that related to the problem? – Özüm Safaoğlu Jun 19 '15 at 19:02
  • @Barry I think this is exactly what you want. No other piece of code is related to the problem, at all. I am quite sure of this. – Özüm Safaoğlu Jun 19 '15 at 19:03
  • You mean I can copy-paste the code you have as is and get it to reproduce the same error? Without including any headers, no `main`, nothing? Cool, what compiler are you using? Need to get me one of those. – Praetorian Jun 19 '15 at 19:10
  • 2
    @ÖzümSafaoğlu This is not a **complete** example. If you say that if you don't use `io_service`, you still get the error, then it's clearly not **minimal** either. – Barry Jun 19 '15 at 19:11
  • 4
    Yes, so ridiculous to make things easier for the people that are trying to help you for free, outrageous really! Is the meaning of *complete* ambiguous somehow? Your example [compiles](http://coliru.stacked-crooked.com/a/6ff446daeaf21f3e) without any errors. Please include what compiler, version, command line arguments, and Boost version you're using. – Praetorian Jun 19 '15 at 19:27
  • I strongly suspect this is a problem with the version of Boost you're using. Which version of Boost are you using? Which compiler? Which operating system? – Max Lybbert Jun 19 '15 at 19:40
  • Hello, turns out it was related to the version of c++. The compiler was using c++ 98 standards. Thank you all for your time. @Praetorian However, you are wrong about help for free. You know what you gain. Human nature does not understand help without gain, even if just to feel good. Especially here, everybody has a cause. Now that i think of it, if I was not long away from the world of c++, and I saw a question like this, first thing i would ask would be the version of c++. But I suppose, that is what you wanna learn when you ask about the compiler. I thank everyone for their answers. – Özüm Safaoğlu Jun 19 '15 at 19:45
  • You got your problem solved and it didn't cost you a penny. So at least in that sense of the word, it was free for you. As for insisting on having a complete example, compare the time difference between your posting of the complete example and getting an answer that solved your problem, to the time between the original post and when you were finally coaxed into providing the complete example. – Praetorian Jun 19 '15 at 20:19

1 Answers1

3

You need to capture io_service by reference to get the above code snippet to compile:

void start_thread(boost::asio::io_service &io_service)
{
    boost::thread tcp_thread(
        [&io_service]() {  // <-- you missed a & here
            io_service.run();
        }
    );
}

Note that the io_service does not implement copy semantics.

Christian Blume
  • 188
  • 1
  • 9