使用带 std::thread 的 SFML 的编译器错误
Compiler errors using SFML with std::thread
(全局)
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)) ===|
(问题)
我不确定我做错了什么,因为这是我第一次使用线程(用于学校项目)。我看过几个示例,包括带有 classes 的示例,但不知何故我还没有设法完成这项工作。
我基本上想制作从一个点开始向上移动直到碰到某物并消失的精灵。所以我想制作一个 class 可以在初始化并调用函数 fire 后自行处理每个对象(在我得到线程工作)。
有人可以给我一些建议,告诉我如何消除上面的最后两个错误并最终使线程工作吗?谢谢。
lasers::fire
取一个 Texture &
。
在这种情况下,编译器不知道如何从 std::thread
构造函数中解析您想要的 lasers::fire
重载,因为您是按值传递它(没有引用)。
用 std::ref(t5)
包裹 t5
以提示编译器您正在通过引用传递它。
std::thread th(&lasers::fire, &zxcv, std::ref(t5), ..., ...);
(全局)
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)) ===|
(问题)
我不确定我做错了什么,因为这是我第一次使用线程(用于学校项目)。我看过几个示例,包括带有 classes 的示例,但不知何故我还没有设法完成这项工作。
我基本上想制作从一个点开始向上移动直到碰到某物并消失的精灵。所以我想制作一个 class 可以在初始化并调用函数 fire 后自行处理每个对象(在我得到线程工作)。
有人可以给我一些建议,告诉我如何消除上面的最后两个错误并最终使线程工作吗?谢谢。
lasers::fire
取一个 Texture &
。
在这种情况下,编译器不知道如何从 std::thread
构造函数中解析您想要的 lasers::fire
重载,因为您是按值传递它(没有引用)。
用 std::ref(t5)
包裹 t5
以提示编译器您正在通过引用传递它。
std::thread th(&lasers::fire, &zxcv, std::ref(t5), ..., ...);