Reputation: 111
(global)
class lasers
{
public:
Sprite sLaser;
int ok2=0;
void fire(Texture &t5, FloatRect bbBG, Vector2f pP)
{
if(ok2!=1)
{
sLaser.setTexture(t5);
sLaser.setOrigin(1,-705);
sLaser.setPosition(pP.x+20.5,pP.y+645);
sLaser.scale(0.1f,0.1f);
ok2=1;
}
while(sLaser.getGlobalBounds().intersects(bbBG))
{
sLaser.move(0,-2);
sleep(milliseconds(10));
}
}
};
(main)
Texture t5;
t5.loadFromFile("images/laser.png");
lasers zxcv;
int j=0;
while (app.isOpen())
{
................
if(Keyboard::isKeyPressed(Keyboard::Space))
if(j==0)
{
thread th(&lasers::fire, &zxcv, t5, boundingBoxBORDER, sPlayer.getPosition());
j=1;
}
................
}
(errors)
||=== Build: Debug in GAME (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int main()':|
\functional||In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (lasers::*)(sf::Texture&, sf::Rect<float>, sf::Vector2<float>)>(lasers, sf::Texture, sf::Rect<float>, sf::Vector2<float>)>':|
\thread|137|required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (lasers::*)(sf::Texture&, sf::Rect<float>, sf::Vector2<float>); _Args = {lasers&, sf::Texture&, sf::Rect<float>&, const sf::Vector2<float>&}]'|
\functional|1665|**error**: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (lasers::*)(sf::Texture&, sf::Rect<float>, sf::Vector2<float>)>(lasers, sf::Texture, sf::Rect<float>, sf::Vector2<float>)>'|
\functional|1695|**error**: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (lasers::*)(sf::Texture&, sf::Rect<float>, sf::Vector2<float>)>(lasers, sf::Texture, sf::Rect<float>, sf::Vector2<float>)>'|
||=== Build failed: 2 error(s), 4 warning(s) (0 minute(s), 1 second(s)) ===|
(Question)
I'm not sure what I'm doing wrong, as this is my first time working with threads(for a school project). I've looked at several examples including ones with classes, but somehow I haven't yet managed to make this work.
I basically want to make sprites which start from a point and go upwards until they hit something and disappear. So I figured making a class could handle every object on its own after it is initialized and the function fire is called(still have to add some things to it after I get the thread to work).
Could someone give me some advice on how to get rid of the last two errors from above and finally make the thread work? Thank you.
Upvotes: 0
Views: 419
Reputation: 21130
lasers::fire
takes a Texture &
.
In this context, the compiler doesn't know how to resolve the overload of lasers::fire
you want from the std::thread
constructor, because you are passing it by value (without a reference).
Wrap t5
with std::ref(t5)
to give the compiler a hint that you are passing it by reference.
std::thread th(&lasers::fire, &zxcv, std::ref(t5), ..., ...);
Upvotes: 3