-
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.
-
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?
-
You didn't do it in the second constructor. The error even says that it's in line 9. ;)
-
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.
-
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){}
-
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
-
If you write a_p(&a) in the initialization list you don't need a_p = &a in the constructor definition.
-
Oh, thanks for telling me!