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

Author Topic: sf::Thread has no default constructor  (Read 6287 times)

0 Members and 1 Guest are viewing this topic.

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
sf::Thread has no default constructor
« 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. 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.

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Thread has no default constructor
« Reply #1 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::Thread has no default constructor
« Reply #2 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. ;)
« Last Edit: August 12, 2013, 10:26:59 pm by G. »

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Thread has no default constructor
« Reply #3 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Thread has no default constructor
« Reply #4 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){}
Laurent Gomila - SFML developer

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Thread has no default constructor
« Reply #5 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Thread has no default constructor
« Reply #6 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.
Laurent Gomila - SFML developer

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: sf::Thread has no default constructor
« Reply #7 on: August 13, 2013, 10:53:16 am »
Oh, thanks for telling me!

 

anything