SFML community forums

Help => System => Topic started by: Shedex on June 22, 2016, 04:06:07 pm

Title: Creating a sf::Thread inside a class
Post by: Shedex on June 22, 2016, 04:06:07 pm
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();
}
};
 
Title: AW: Creating a sf::Thread inside a class
Post by: eXpl0it3r on June 22, 2016, 04:10:48 pm
Check the documentation for how to use sf::Thread
Title: Re: AW: Creating a sf::Thread inside a class
Post by: Shedex on June 22, 2016, 04:14:12 pm
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
Title: Re: Creating a sf::Thread inside a class
Post by: Laurent on June 22, 2016, 04:18:54 pm
You should have a look at the following sf::Thread tutorial sections:
- "How to create a thread" -> member functions
- "Common mistakes"
Title: Re: Creating a sf::Thread inside a class
Post by: Shedex on June 22, 2016, 04:56:03 pm
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?
Title: Re: Creating a sf::Thread inside a class
Post by: Laurent on June 22, 2016, 05:54:39 pm
Quote
- "How to create a thread" -> member functions

-->

Quote
- Member function:

class MyClass
{
public:

    void func()
    {
    }
};

MyClass object;
sf::Thread thread(&MyClass::func, &object);

What's so hard about reading tutorials? :P
Title: Re: Creating a sf::Thread inside a class
Post by: Hapax on June 22, 2016, 10:10:03 pm
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
Title: Re: Creating a sf::Thread inside a class
Post by: Laurent on June 22, 2016, 10:17:12 pm
Quote
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;
};

Quote
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.

Quote
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.

Quote
Just an idea
Please don't throw random ideas, just let the OP read the tutorial...
Title: Re: Creating a sf::Thread inside a class
Post by: Hapax on June 22, 2016, 10:51:19 pm
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?
Title: Re: Creating a sf::Thread inside a class
Post by: Laurent on June 22, 2016, 10:55:40 pm
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.