0

I have my class:

class MyClass
{
public:
    MyClass();
    list<Obj> mainList;
    void MyMethod(){//This method changes elements in mainList};
}

void StartThread(MyClass& tmp)
{
    tmp.MyMethod();
}

main()
{
    MyClass test();
    std::thread thr(StartThread, std::ref(test));
    the.join(); //or detach(), both don't work
}

So can I do it this way using C++11?

The error that I get every time(LongPollSession = MyClass):

/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(LongPollSession); _Args = {std::reference_wrapper<LongPollSession*>}]’
main.cpp:19:47:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<LongPollSession*>))(LongPollSession)>’
         _M_invoke(_Index_tuple<_Indices...>)
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 3
    `MyClass test();` -> `MyClass test;` – Piotr Skotnicki Nov 29 '15 at 14:02
  • ``MyClass test();`` is one of the "funny" things in C++. It has syntax of a function declaration as well as it can be "create instance of MyClass with standard constructor". As the latter is what you wish to do, better write ``MyClass test;`` (without the brackets). – BitTickler Nov 29 '15 at 14:05
  • @PiotrSkotnicki: ... or, as this is C++11, `MyClass test();` -> `MyClass test{};` (and for a bit of explanation: `MyClass test();` is a function declaration; it is variation of [the Most Vexing Parse](https://en.wikipedia.org/wiki/Most_vexing_parse)). – Dietmar Kühl Nov 29 '15 at 14:06
  • In addition to the issue about the Most Vexing Parse there are a lot of typos in the code preventing compilation, too. – Dietmar Kühl Nov 29 '15 at 14:10
  • @PiotrSkotnicki Fine, thank you, i have changed to `Myclass test = MyClass()` and it works – Andrew Kusachev Nov 29 '15 at 14:19

0 Answers0