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

Author Topic: Thread prob  (Read 6304 times)

0 Members and 1 Guest are viewing this topic.

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Thread prob
« on: August 27, 2010, 06:10:26 am »
Hello,

I have been trying to call a thread in a class using a function from an inherited class like this:

Code: [Select]
sf::Thread Thread(&Attacks[Index].Start_Animation());

However, I get the following error:

Code: [Select]
C:\Users\Steven\Desktop\RPG\PC.cpp|225|error: request for member `Start_Animation' in `((PC*)this)->PC::Attacks[((int)Index)]', which is of non-class type `Attack*'|

Function works when its not in a thread, such as:

Code: [Select]
Attacks[Index].Start_Animation()

The 2 classes look like this (A caracter has x number of attacks)

Code: [Select]

class Attack
{
    protected:
    public:
              void Start_Animation(void* UserData);
};
class PC
{
    protected:
                 Attack *Attacks[10];
    public:
}

Any suggestion? I am pretty sure I am just missing a coma or something.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread prob
« Reply #1 on: August 27, 2010, 08:09:23 am »
You can only use non-member functions taking a void* as argument.
Read the tutorial for more details.
Laurent Gomila - SFML developer

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: Thread prob
« Reply #2 on: August 27, 2010, 10:23:54 am »
Quote from: "Xenavar"
Code: [Select]
sf::Thread Thread(&Attacks[Index].Start_Animation());

However, I get the following error:

Code: [Select]
C:\Users\Steven\Desktop\RPG\PC.cpp|225|error: request for member `Start_Animation' in `((PC*)this)->PC::Attacks[((int)Index)]', which is of non-class type `Attack*'|


You are starting a thread using what Start_Animation returns (which is a Attack pointer) and not the function itself. Skip the extra ()

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread prob
« Reply #3 on: August 27, 2010, 10:40:34 am »
Quote
Skip the extra ()

This is not enough, C++ is not that flexible, only this kind of function will work
Code: [Select]
void thread_func(void*);
Laurent Gomila - SFML developer

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Thread prob
« Reply #4 on: August 27, 2010, 01:35:47 pm »
You could simply wrap the member function though:
Code: [Select]
void callmember(void* number) {
    Attacks[*(static_cast<int*>(number))].Start_Animation();
}

And then call callmember as a thread.[/code]

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Thread prob
« Reply #5 on: August 27, 2010, 05:14:12 pm »
Thank you for your replys, its all working except when I try to call the thread in class "PC" I have the following error:

C:\Users\Steven\Desktop\RPG\PC.cpp|226|error: no matching function for call to `sf::Thread::Thread(void (PC::*)(void*, int), short int&)'|

Code is:

Code: [Select]

void PC::Handle_event(sf::Event Event, sf::RenderWindow &App)
{
   sf::Thread Thread(&PC::Start_Animation, Index);   <----------------------
}

void PC::Start_Animation(void* UserData, int Index)
{
    Attacks[Index]->Next(Sprite,Facing);
}


At first I tryed to remove the PC:: in front of the function but it told me to put it back (meaning sf::Thread Thread(&Start_Animation, Index);   doesn't work either)

What am I missing here?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread prob
« Reply #6 on: August 27, 2010, 06:01:48 pm »
This is just the third time that I say it :D

The function that you pass to sf::Thread must have this prototype
Code: [Select]
void thread_func(void*);
And nothing else.

Yours is:
Code: [Select]
void PC::thread_func(void*, int);

So:
- make it a non-member function (or a static member)
- remove the int parameter

But after looking at your code I can give you a much better answer: don't use a thread for animating a sprite, this is the wrong way of doing it. You should rather update your animation in the main thread, when your main loop calls the update function of your entity (or whatever that updates its state every frame).
Laurent Gomila - SFML developer

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Thread prob
« Reply #7 on: August 27, 2010, 06:45:00 pm »
Heh, sorry for forcing you to repeat I am french and we don't use the same terms. But ive fixed my problem without using threads and its smooth.

Thank you very much again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread prob
« Reply #8 on: August 27, 2010, 06:50:59 pm »
Quote
Heh, sorry for forcing you to repeat I am french and we don't use the same terms

I'm french too, and there's a french forum here ;)
http://www.sfml-dev.org/forum-fr/
Laurent Gomila - SFML developer

 

anything