【发布时间】: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