0

(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.

14
  • Get the real compiler messages, not that mess from your IDE. Commented Jan 13, 2018 at 9:38
  • 1
    Simplify your code. Boil it down to ONLY the minimal code required to produce the error message. Commented Jan 13, 2018 at 9:40
  • 2
    t5 -> std::ref(t5). But your app won't work still. Commented Jan 13, 2018 at 9:40
  • Is it not better to leave it all there so people can compile it? Initally I also wanted to do as you said. @JohnZwinck Commented Jan 13, 2018 at 9:42
  • 1
    We only need to compile the relevant components of the line that is giving you the issue -- which is whatever is related to the line that creates thread t1. It takes a bit to sift through the code that's superfluous. Commented Jan 13, 2018 at 9:43

1 Answer 1

3

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), ..., ...);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, it built now finally.
So after getting rid of the errors, the program crashes for some reason whenever I press "Space"(it's related to the thread for sure, as I've tried running the program without the "while" in the fire() function as well). Could you please tell me why do you think that happened?
You're entering the world of multi-threading: thread-safety, race conditions, etc. It's impossible to debug your code without it in front of us. It doesn't make much sense to spawn a thread each frame. Calling sleep within the fire while loop is asking for a race-condition. Why are you using threads to begin with? Is it a requirement?
I see, that's quite beyond my current knowledge it seems. Threads are not a requirement, but they just seemed like the suitable thing to use. I wanted to create a "lasers" object each time "Space" was pressed(using some array) and call the fire() function. Than I would just add everything I need in my fire() function, like when to have the object to stop moving or be drawn on the window, and thus I wouldn't have to touch that object ever again in the main. I guess I'll just change the approach... Perhaps I'll try something with dynamic memory. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.