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

Author Topic: 1.6 Threads when using classes  (Read 4069 times)

0 Members and 1 Guest are viewing this topic.

Slasher133

  • Newbie
  • *
  • Posts: 2
    • View Profile
1.6 Threads when using classes
« 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:
Code: [Select]
error: no matching function for call to 'sf::Thread::Thread(void (Foo::*)(void*))'|

Main
Code: [Select]
#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
Code: [Select]
#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
Code: [Select]
#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();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: 1.6 Threads when using classes
« Reply #1 on: April 06, 2012, 09:30:03 am »
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.
Laurent Gomila - SFML developer

Slasher133

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 1.6 Threads when using classes
« Reply #2 on: April 06, 2012, 05:54:49 pm »
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.

 

anything