SFML community forums
Help => System => Topic started by: Slasher133 on April 06, 2012, 07:07:06 am
-
I'm not sure what I am doing wrong, all I want is to be able to use the thread example but in a different class.
Here are a couple of the errors I get:
error: no matching function for call to 'sf::Thread::Thread(void (Foo::*)(void*))'|
Main
#include <SFML/System.hpp>
#include <iostream>
#include "Foo.h"
int main()
{
// Create a thread with our function
// Start it !
Foo bar;
bar.init();
// Print something...
for (int i = 0; i < 10; ++i)
std::cout << "I'm the main thread" << std::endl;
return EXIT_SUCCESS;
}
Foo.h
#ifndef FOO
#define FOO
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
class Foo{
public:
void ThreadFunction(void*);
void init();
};
#endif
Foo.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include "Foo.h"
void Foo::ThreadFunction(void* UserData)
{
// Print something...
for (int i = 0; i < 10; ++i)
std::cout << "I'm the thread number 1" << std::endl;
}
void Foo::init(){
sf::Thread Thread(&ThreadFunction);
//Tried these as well, did not work
//sf::Thread Thread(&Foo::ThreadFunction);
//sf::Thread Thread(ThreadFunction());
Thread.Launch();
}
-
Member functions are not non-member functions. The big difference is that the former requires an instance of the class to be called.
If you want to pass a member function to sf::Thread, you must use inheritance as shown in the tutorial.
-
Oh, I see, thanks for the help. I was just thinking that using that base class tutorial was for something else, I did not realize it was for what I wanted to do, and now it works properly.