Hello everyone,
I'm trying to start a member function in a new thread, which works fine, but i can not manage to use a render window as a parameter of the member function. Here is an small example of what i'm struggling with:
This code works perfectly fine:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <thread>
class ClassWithThread
{
public:
ClassWithThread()
: m_thread(&ClassWithThread::f, this)
{
i = 4;
}
void f()
{
std::cout << i << std::endl;
}
private:
std::thread m_thread;
int i;
};
int main (){
ClassWithThread x;
x.f();
return 0;
}
But if i try to use a render window inside the member function, i get a long error message that i don't really understand:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <thread>
class ClassWithThread
{
public:
ClassWithThread()
: m_thread(&ClassWithThread::f, this)
{
i = 4;
}
void f(sf::RenderWindow& window)
{
std::cout << i << std::endl;
}
private:
std::thread m_thread;
int i;
};
int main (){
ClassWithThread x;
// create the window
sf::RenderWindow window(sf::VideoMode(1920, 1080), "MyWindow");
x.f(window);
return 0;
}
With the error (sorry, some of it is in german):
g++ -pthread -o program -lsfml-graphics -lsfml-window -lsfml-system main.cpp
In file included from main.cpp:4:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >’:
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ClassWithThread::*)(sf::RenderWindow&); _Args = {ClassWithThread*}]’
main.cpp:11:41: required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >::_M_invoke(std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >::_Indices)’
operator()()
^~~~~~~~
/usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*>]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/include/c++/7/thread:231:4: note: template argument deduction/substitution failed:
/usr/include/c++/7/thread: In substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’:
/usr/include/c++/7/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >’
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ClassWithThread::*)(sf::RenderWindow&); _Args = {ClassWithThread*}]’
main.cpp:11:41: required from here
/usr/include/c++/7/thread:233:29: error: no matching function for call to ‘__invoke(std::__tuple_element_t<0, std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >, std::__tuple_element_t<1, std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >)’
-> decltype(std::__invoke(_S_declval<_Ind>()...))
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/tuple:41:0,
from /usr/include/c++/7/bits/unique_ptr.h:37,
from /usr/include/c++/7/bits/locale_conv.h:41,
from /usr/include/c++/7/locale:43,
from /usr/include/SFML/System/Utf.hpp:33,
from /usr/include/SFML/System/String.hpp:32,
from /usr/include/SFML/System.hpp:42,
from /usr/include/SFML/Window.hpp:32,
from /usr/include/SFML/Graphics.hpp:32,
from main.cpp:1:
/usr/include/c++/7/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
__invoke(_Callable&& __fn, _Args&&... __args)
^~~~~~~~
/usr/include/c++/7/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/include/c++/7/bits/invoke.h: In substitution of ‘template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void (ClassWithThread::*)(sf::RenderWindow&); _Args = {ClassWithThread*}]’:
/usr/include/c++/7/thread:233:29: required by substitution of ‘template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1}]’
/usr/include/c++/7/thread:240:2: required from ‘struct std::thread::_Invoker<std::tuple<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*> >’
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ClassWithThread::*)(sf::RenderWindow&); _Args = {ClassWithThread*}]’
main.cpp:11:41: required from here
/usr/include/c++/7/bits/invoke.h:89:5: error: no type named ‘type’ in ‘struct std::__invoke_result<void (ClassWithThread::*)(sf::RenderWindow&), ClassWithThread*>’
makefile:2: die Regel für Ziel „all“ scheiterte
make: *** [all] Fehler 1
Kompilierung fehlgeschlagen.
I already tried to commit the window as a pointer or as a reference. I'm using linux debian testing with g++ 4:7.2.0-1d1 and libsfml 2.4.
Has anyone an idea what i'm doing wrong?