SFML community forums

Help => System => Topic started by: Inflammatory Nugget on August 12, 2013, 09:30:46 pm

Title: sf::Thread has no default constructor
Post by: Inflammatory Nugget on August 12, 2013, 09:30:46 pm
Greetings,

I was trying to create a class in a separate header. As soon as the constructor for the class would be called, a thread inside that class would run. However, declaring a sf::Thread in a class gives some issues. So I tried to search the web and  I found this forum thread: http://en.sfml-dev.org/forums/index.php?topic=7906.msg52768#msg52768 (http://en.sfml-dev.org/forums/index.php?topic=7906.msg52768#msg52768). I attempted what was suggested in this thread:
#include <SFML\System.hpp>
#include <iostream>
class test_class{
private:
        test_class():test_thread(&test_class::loop, this){}
        int *a_p;
        sf::Thread test_thread;
public:
        test_class::test_class(int& a){
                a_p = &a;
        }
        void loop(){
                while(*a_p < 50){
                        std::cout << *a_p << std::endl;
                }
        }
};
However, sf::Thread has no default constructor, so I got the following error:
]1>c:\users\frank\documents\visual studio 2010\projects\cppapplication_4\test.h(9): error C2512: 'sf::Thread' : no appropriate default constructor available
I'm sort of confused what to do next  :-\

Any help will be appreciated.
Title: Re: sf::Thread has no default constructor
Post by: Inflammatory Nugget on August 12, 2013, 10:22:23 pm
Quote from: The thread tutorial, surprisingly... ( http://www.sfml-dev.org/tutorials/2.1/system-thread.php )
If you want to use a sf::Thread inside a class, don't forget that it doesn't have a default constructor. Therefore, you have to initialize it directly in the constructor's initialization list
It's also written by Laurent in the forum thread that you linked, with a little code example. :o
Yes, that's... what I did?
Title: Re: sf::Thread has no default constructor
Post by: G. on August 12, 2013, 10:24:54 pm
You didn't do it in the second constructor. The error even says that it's in line 9. ;)
Title: Re: sf::Thread has no default constructor
Post by: Inflammatory Nugget on August 12, 2013, 11:02:35 pm
Er, how exactly? The tutorial nor the thread I linked to tells me how to. Maybe I'm just not awake enough to do this  :-\

Thanks for the swift responses btw, Mr Geedot.
Title: Re: sf::Thread has no default constructor
Post by: Laurent on August 12, 2013, 11:09:59 pm
Quote
Er, how exactly?
The same way as you did in the other constructor :P

test_class::test_class(int& a):test_thread(&test_class::loop, this),a_p(&a){}
Title: Re: sf::Thread has no default constructor
Post by: Inflammatory Nugget on August 13, 2013, 08:57:31 am
Good morning,

My program runs perfectly fine now. Here's what it looks like:
#include <SFML\System.hpp>
#include <iostream>
class test_class{
private:
        test_class():test_thread(&test_class::loop, this){}
        int *a_p;
public:
        test_class::test_class(int& a):test_thread(&test_class::loop, this),a_p(&a){
                a_p = &a;
        }
        void loop(){
                while(*a_p < 50){
                        std::cout << *a_p << std::endl;
                }
        }
        sf::Thread test_thread;
};
I thank you for your help :D
Title: Re: sf::Thread has no default constructor
Post by: Laurent on August 13, 2013, 09:30:02 am
If you write a_p(&a) in the initialization list you don't need a_p = &a in the constructor definition.
Title: Re: sf::Thread has no default constructor
Post by: Inflammatory Nugget on August 13, 2013, 10:53:16 am
Oh, thanks for telling me!