Rentian Dong
Rentian Dong

Reputation: 101

c++11 compile failed due to issues with templates and thread

I am trying to create a new thread by following this tutorial http://www.cplusplus.com/reference/thread/thread/

And following are the relevant code I wrote

Game.cpp:

Game :: Game (bool isDebug, bool startNew) {
    // ...
    std :: thread gameThread(Game :: tick);
}

void Game :: tick() {
    std :: this_thread :: sleep_for(std :: chrono :: seconds (1));
    funds += getIncomeRate();
}

Game.hpp:

#ifndef Game_hpp
#define Game_hpp

#include <thread>

using namespace std;

class Game{
    public:
    // ...
        Game (bool, bool);
        void tick();
};

#endif /* Game_hpp */

The error I get during compiling says

17:37:19 **** Incremental Build of configuration Debug for project Store Management Game ****
Info: Internal Builder is used for build
g++ -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o Game.o "..\\Game.cpp" 
In file included from C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:39:0,
                 from ..\Game.hpp:16,
                 from ..\Game.cpp:9:
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional: In instantiation of 'struct std::_Bind_check_arity<void (Game::*)()>':
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1538:12:   required from 'struct std::_Bind_simple_helper<void (Game::*)()>'
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1552:5:   required by substitution of 'template<class _Callable, class ... _Args> typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]'
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:142:59:   required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]'
..\Game.cpp:33:46:   required from here
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1426:7: error: static assertion failed: Wrong number of arguments for pointer-to-member
       static_assert(_Varargs::value
       ^
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional: In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Game::*)()>()>':
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/thread:142:59:   required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Game::*)(); _Args = {}]'
..\Game.cpp:33:46:   required from here
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1505:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Game::*)()>()>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
C:/Program Files/Haskell Platform/8.0.1/mingw/include/c++/5.2.0/functional:1526:9: error: no type 
named 'type' in 'class std::result_of<std::_Mem_fn<void (Game::*)()>()>'
         _M_invoke(_Index_tuple<_Indices...>)
         ^

17:37:20 Build Finished (took 583ms)

I am not very familiar with C++ and templates in general, and I really do not understand what is wrong with my code. Apologize in advance if this would be a trivial question.

Upvotes: 1

Views: 1237

Answers (2)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84169

Since the method you are wrapping into a thread is not static (and not a free function) you need to give the thread constructor a reference to the class instance.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 477040

You want:

std::thread gameThread(&Game::tick, this);
//                    ^^^           ^^^^

Upvotes: 3

Related Questions