【问题标题】:Compiler errors using SFML with std::thread使用带有 std::thread 的 SFML 的编译器错误
【发布时间】:2018-06-22 15:25:05
【问题描述】:

(全球)

    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));
            }


        }
    };     

(主要)

    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;
            }
        ................
    }

(错误)

||=== 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)) ===|

(问题)

我不确定自己做错了什么,因为这是我第一次使用线程(针对学校项目)。我查看了几个示例,包括带有类的示例,但不知何故我还没有设法完成这项工作。

我基本上想制作从一个点开始向上移动直到它们撞到某物并消失的精灵。所以我认为创建一个类可以在初始化并调用函数 fire 后自行处理每个对象(在我让线程工作后仍然需要向它添加一些东西)。

有人能给我一些关于如何摆脱上面最后两个错误并最终使线程工作的建议吗?谢谢。

【问题讨论】:

  • 获取真正的编译器消息,而不是你的 IDE 中的那些混乱。
  • 简化您的代码。将其归结为仅生成错误消息所需的最少代码。
  • t5 -> std::ref(t5)。但是您的应用程序将无法运行。
  • 把它全部放在那里以便人们可以编译不是更好吗?一开始我也想按照你说的去做。 @JohnZwinck
  • 我们只需要编译给您问题的行的相关组件——即与创建线程t1 的行相关的任何内容。筛选多余的代码需要一点时间。

标签: c++ multithreading c++11 sfml


【解决方案1】:

lasers::fire 采用Texture &amp;

在这种情况下,编译器不知道如何从 std::thread 构造函数中解决您想要的 lasers::fire 重载问题,因为您是按值传递它(没有引用)。

std::ref(t5) 包裹t5 以向编译器提示您正在通过引用传递它。

std::thread th(&lasers::fire, &zxcv, std::ref(t5), ..., ...);

【讨论】:

  • 非常感谢,终于建好了。
  • 所以在消除错误后,每当我按“空格”时,程序都会因某种原因崩溃(它肯定与线程有关,因为我尝试在没有“while”的情况下运行程序在 fire() 函数中也是如此)。你能告诉我你为什么会这样吗?
  • 您正在进入多线程的世界:线程安全、竞争条件等。如果没有它在我们面前调试您的代码是不可能的。每帧生成一个线程没有多大意义。在fire while 循环中调用 sleep 是要求一个竞争条件。为什么你一开始就使用线程?有要求吗?
  • 我明白了,这似乎超出了我目前的知识范围。线程不是必需的,但它们似乎适合使用。每次按下“Space”(使用一些数组)并调用 fire() 函数时,我想创建一个“lasers”对象。比我只需在我的 fire() 函数中添加我需要的所有内容,例如何时让对象停止移动或在窗口上绘制,因此我不必触摸那个对象再次在 main 中。我想我会改变方法......也许我会尝试使用动态内存。感谢您的帮助。
猜你喜欢
  • 2015-03-25
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
相关资源
最近更新 更多