Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a sf::Thread inside a class  (Read 8201 times)

0 Members and 2 Guests are viewing this topic.

Shedex

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Creating a sf::Thread inside a class
« 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();
}
};
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Creating a sf::Thread inside a class
« Reply #1 on: June 22, 2016, 04:10:48 pm »
Check the documentation for how to use sf::Thread
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Shedex

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: AW: Creating a sf::Thread inside a class
« Reply #2 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating a sf::Thread inside a class
« Reply #3 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"
Laurent Gomila - SFML developer

Shedex

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Creating a sf::Thread inside a class
« Reply #4 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating a sf::Thread inside a class
« Reply #5 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
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a sf::Thread inside a class
« Reply #6 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating a sf::Thread inside a class
« Reply #7 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...
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a sf::Thread inside a class
« Reply #8 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Creating a sf::Thread inside a class
« Reply #9 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.
Laurent Gomila - SFML developer

 

anything