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

Author Topic: sf :: Thread inside a class  (Read 7616 times)

0 Members and 1 Guest are viewing this topic.

fiberglass22

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
sf :: Thread inside a class
« on: May 13, 2012, 09:28:29 pm »
Hi everyone. I'm new to SFML and self-taught so be nice :). Anyways, I'm having some difficulties putting a sf::Thread inside of a class. This is my code:
//file: shapecontainer.h
#ifndef SHAPECONTAINER_H
#define SHAPECONTAINER_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
class shapeContainer {
    void threadFunction ( void * UserData );
    sf :: Thread runThread ( & shapeContainer :: threadFunction );
    private:
        int type , xl1 , xl2 , yl1 , yl2;
    public:
        shapeContainer ( );
        void setType ( int input );
        int getType ( );
        void setLocation ( int x1 , int y1 , int x2 , int y2 );
        sf :: RenderWindow * drawShape ( sf :: RenderWindow * window , sf :: Color color );
};
#endif
//file: shapecontainer.cpp (trimmed)
#include <iostream>
#include "shapecontainer.h"
#include "gamewindow.h"
shapeContainer :: shapeContainer ( ) {
}
void shapeContainer :: threadFunction ( void * UserData ) {
    std :: cout << "test\n";
}
 
And the compiler error:
Code: [Select]
In file included from ./main.cpp:2:
./shapecontainer.h:7: error: expected identifier before ‘&’ token
./shapecontainer.h:7: error: expected ‘,’ or ‘...’
In file included from ./gamewindow.cpp:2:
./shapecontainer.h:7: error: expected identifier before ‘&’ token
./shapecontainer.h:7: error: expected ‘,’ or ‘...’
In file included from ./shapecontainer.cpp:2:
./shapecontainer.h:7: error: expected identifier before ‘&’ token
./shapecontainer.h:7: error: expected ‘,’ or ‘...’
Thanks in advance.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: sf :: Thread inside a class
« Reply #1 on: May 13, 2012, 09:38:57 pm »
The & symbol there doesn't make sense in a function declaration (at least not when putting it front).

Did you want the runThread function to take a pointer?
In this case it should be: "sf :: Thread runThread  ( shapeContainer :: threadFunction * ) "

If you wanted it to take a reference then the & should be where the * is in the above line.


EDIT: It looked like a function to me, but I guess runThread is just an object.
In this case the mistake is that you can't call its constructor from inside the class.
« Last Edit: May 13, 2012, 09:45:39 pm by texus »
TGUI: C++ SFML GUI

fiberglass22

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sf :: Thread inside a class
« Reply #2 on: May 13, 2012, 09:46:32 pm »
The & symbol there doesn't make sense in a function declaration (at least not when putting it front).

Did you want the runThread function to take a pointer?
In this case it should be: "sf :: Thread runThread  ( shapeContainer :: threadFunction * ) "

If you wanted it to take a reference then the & should be where the * is in the above line.
I tried this:
#ifndef SHAPECONTAINER_H
#define SHAPECONTAINER_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
class shapeContainer {
    void threadFunction ( void * UserData );
    sf :: Thread runThread ( shapeContainer :: threadFunction * );
 
which resulted in this:
Code: [Select]
In file included from ./main.cpp:1:
./shapecontainer.h:7: error: ‘shapeContainer::threadFunction’ is not a type
In file included from ./gamewindow.cpp:1:
./shapecontainer.h:7: error: ‘shapeContainer::threadFunction’ is not a type
In file included from ./shapecontainer.cpp:2:
./shapecontainer.h:7: error: ‘shapeContainer::threadFunction’ is not a type

fiberglass22

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sf :: Thread inside a class
« Reply #3 on: May 13, 2012, 09:49:06 pm »
EDIT: It looked like a function to me, but I guess runThread is just an object.
In this case the mistake is that you can't call its constructor from inside the class.
Oh, alright. What would be a good way to integrate a sf::Thread into a class then? I have a class for animated shapes, and each shape requires a thread to loop it's animation.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf :: Thread inside a class
« Reply #4 on: May 13, 2012, 09:57:16 pm »
Quote
I have a class for animated shapes, and each shape requires a thread to loop it's animation.
Oh my god, don't do this :o

By the way, the solution is to use the initialization list:
class shapeContainer
{
public:
    shapeContainer() : runthread(&Test::threadFunction, this)
    {
    }

private:
    void threadFunction()
    {
        ...
    }

    sf::Thread runthread;
};
Laurent Gomila - SFML developer

fiberglass22

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: sf :: Thread inside a class
« Reply #5 on: May 13, 2012, 10:10:27 pm »
...
Hah, thanks. I'll use this.

EDIT: No luck, unfortunately. Here's the updated code:
#ifndef SHAPECONTAINER_H
#define SHAPECONTAINER_H
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
class shapeContainer {
    public:
        shapeContainer ( ) : runthread ( & shapeContainer :: threadFunction , this ) { }
        shapeContainer ( );
        void setType ( int input );
        int getType ( );
        void setLocation ( int x1 , int y1 , int x2 , int y2 );
        sf :: RenderWindow * drawShape ( sf :: RenderWindow * window , sf :: Color color );
    private:
        void threadFunction ( ) { }
        int type , xl1 , xl2 , yl1 , yl2;
        sf::Thread runthread;
};
#endif
and the output:
Code: [Select]
In file included from ./main.cpp:1:
./shapecontainer.h:8: error: ‘shapeContainer::shapeContainer()’ cannot be overloaded
./shapecontainer.h:7: error: with ‘shapeContainer::shapeContainer()’
./shapecontainer.h: In constructor ‘shapeContainer::shapeContainer()’:
./shapecontainer.h:7: error: no matching function for call to ‘sf::Thread::Thread(void (shapeContainer::*)(), shapeContainer* const)’
/usr/include/SFML/System/Unix/Thread.hpp:92: note: candidates are: sf::Thread::Thread()
/usr/include/SFML/System/Unix/Thread.hpp:57: note:                 sf::Thread::Thread(void (*)(void*), void*)
/usr/include/SFML/System/Unix/Thread.hpp:45: note:                 sf::Thread::Thread(const sf::Thread&)
In file included from ./gamewindow.cpp:1:
./shapecontainer.h:8: error: ‘shapeContainer::shapeContainer()’ cannot be overloaded
./shapecontainer.h:7: error: with ‘shapeContainer::shapeContainer()’
./shapecontainer.h: In constructor ‘shapeContainer::shapeContainer()’:
./shapecontainer.h:7: error: no matching function for call to ‘sf::Thread::Thread(void (shapeContainer::*)(), shapeContainer* const)’
/usr/include/SFML/System/Unix/Thread.hpp:92: note: candidates are: sf::Thread::Thread()
/usr/include/SFML/System/Unix/Thread.hpp:57: note:                 sf::Thread::Thread(void (*)(void*), void*)
/usr/include/SFML/System/Unix/Thread.hpp:45: note:                 sf::Thread::Thread(const sf::Thread&)
In file included from /usr/include/SFML/System/Thread.hpp:40,
                 from /usr/include/SFML/System.hpp:38,
                 from /usr/include/SFML/Window.hpp:32,
                 from /usr/include/SFML/Graphics.hpp:32,
                 from ./shapecontainer.h:3,
                 from ./gamewindow.cpp:1:
/usr/include/SFML/System/NonCopyable.hpp: In member function ‘sf::Thread& sf::Thread::operator=(const sf::Thread&)’:
/usr/include/SFML/System/NonCopyable.hpp:64: error: ‘sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)’ is private
/usr/include/SFML/System/Unix/Thread.hpp:45: error: within this context
In file included from ./gamewindow.cpp:1:
./shapecontainer.h: In member function ‘shapeContainer& shapeContainer::operator=(const shapeContainer&)’:
./shapecontainer.h:5: note: synthesized method ‘sf::Thread& sf::Thread::operator=(const sf::Thread&)’ first required here
./gamewindow.cpp: In member function ‘void GameWindow::drawGameWindow(sf::RenderWindow*, sf::Color, int, int, int)’:
./gamewindow.cpp:13: note: synthesized method ‘shapeContainer& shapeContainer::operator=(const shapeContainer&)’ first required here
In file included from /usr/include/SFML/System/Thread.hpp:40,
                 from /usr/include/SFML/System.hpp:38,
                 from /usr/include/SFML/Window.hpp:32,
                 from /usr/include/SFML/Graphics.hpp:32,
                 from ./shapecontainer.h:3,
                 from ./gamewindow.cpp:1:
/usr/include/SFML/System/NonCopyable.hpp: In copy constructor ‘sf::Thread::Thread(const sf::Thread&)’:
/usr/include/SFML/System/NonCopyable.hpp:57: error: ‘sf::NonCopyable::NonCopyable(const sf::NonCopyable&)’ is private
/usr/include/SFML/System/Unix/Thread.hpp:45: error: within this context
In file included from ./gamewindow.cpp:1:
./shapecontainer.h: In copy constructor ‘shapeContainer::shapeContainer(const shapeContainer&)’:
./shapecontainer.h:5: note: synthesized method ‘sf::Thread::Thread(const sf::Thread&)’ first required here
./gamewindow.cpp: In member function ‘shapeContainer GameWindow::shapeAt(int, int)’:
./gamewindow.cpp:40: note: synthesized method ‘shapeContainer::shapeContainer(const shapeContainer&)’ first required here
In file included from ./shapecontainer.cpp:2:
./shapecontainer.h:8: error: ‘shapeContainer::shapeContainer()’ cannot be overloaded
./shapecontainer.h:7: error: with ‘shapeContainer::shapeContainer()’
./shapecontainer.h: In constructor ‘shapeContainer::shapeContainer()’:
./shapecontainer.h:7: error: no matching function for call to ‘sf::Thread::Thread(void (shapeContainer::*)(), shapeContainer* const)’
/usr/include/SFML/System/Unix/Thread.hpp:92: note: candidates are: sf::Thread::Thread()
/usr/include/SFML/System/Unix/Thread.hpp:57: note:                 sf::Thread::Thread(void (*)(void*), void*)
/usr/include/SFML/System/Unix/Thread.hpp:45: note:                 sf::Thread::Thread(const sf::Thread&)
./shapecontainer.cpp: At global scope:
./shapecontainer.cpp:4: error: redefinition of ‘shapeContainer::shapeContainer()’
./shapecontainer.h:7: error: ‘shapeContainer::shapeContainer()’ previously defined here
« Last Edit: May 13, 2012, 10:24:47 pm by fiberglass22 »

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: sf :: Thread inside a class
« Reply #6 on: May 14, 2012, 05:47:57 pm »
You're defining the same shapeContainer constructor twice in lines 7 and 8.
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

 

anything