-
I have a class with function a and function b. If function a is called, it should create a Thread to function b and then launch it, but I got the error "no appropiate default constructor available". Heres my code:
Class test {
public:
void b() {
//do stuff...
}
void a() {
sf::Thread thread(&b);
thread.launch();
}
};
-
Check the documentation for how to use sf::Thread
-
Check the documentation for how to use sf::Thread
I did, but I didnt find the mistake. A code example would be nice, because Im a beginner
-
You should have a look at the following sf::Thread tutorial sections:
- "How to create a thread" -> member functions
- "Common mistakes"
-
You should have a look at the following sf::Thread tutorial sections:
- "How to create a thread" -> member functions
- "Common mistakes"
I declared it now as a pointer in the private section and created a new instance inside the function, but I still get the same error, even when not declaring it locally:
'&' : illegal operation on bound member function expression
'sf::Thread' : no appropriate default constructor available
Class test {
private:
sf::Thread* thread;
public:
void b() {
//do stuff...
}
void a() {
thread = new sf::Thread(&b)
thread.launch();
}
};
I may be stupid but I still dont see the mistake...can you give me a short piece of code that explains it better or that works?
-
- "How to create a thread" -> member functions
-->
- Member function:
class MyClass
{
public:
void func()
{
}
};
MyClass object;
sf::Thread thread(&MyClass::func, &object);
What's so hard about reading tutorials? :P
-
It looks like (albeit quite hidden due to the absence of any code indentation) they want the thread to be contained within the class itself. There are, of course, examples for that too in the tutorial. However, it is created only when the a() function is executed.
My angle here is that maybe it fails because b is out of scope so the way to address it would be via this.
Just an idea. I haven't used sf::Thread at all. :P
-
It looks like (albeit quite hidden due to the absence of any code indentation) they want the thread to be contained within the class itself. There are, of course, examples for that too in the tutorial.
Indeed, there's code that better matches what the OP is doing.
class ClassWithThread
{
public:
ClassWithThread()
: m_thread(&ClassWithThread::f, this)
{
}
private:
void f()
{
...
}
sf::Thread m_thread;
};
However, it is created only when the a() function is executed.
That's more a consequence of the OP not knowing how to do it ;)
But with dynamic allocation it can also work like this anyway.
My angle here is that maybe it fails because b is out of scope so the way to address it would be via this.
The only reason it fails is because the syntax is incorrect and the tutorial show the correct one.
Just an idea
Please don't throw random ideas, just let the OP read the tutorial...
-
My apologies for the "random idea". I don't have the ability to test/try anything at the moment.
Just to be clear, does this mean that a thread cannot be created on the stack inside a class?
-
The sf::Thread instance has to live at least as long as the function that it runs. You can't just let the thread run without its owner sf::Thread instance to control it. If you really want to do so, use std::thread. Everyone should use std::thread anyway: it's standard, more complete than sf::Thread, and supported by all compilers in 2016.