使用 SFML 和 std::thread 的编译器错误
Compiler errors using SFML with std::thread
(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));
}
}
};
(主(
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 后自行处理每个对象(在我让线程工作后仍然必须向其添加一些东西(。
有人可以给我一些关于如何摆脱上面的最后两个错误并最终使线程工作的建议吗?谢谢。
lasers::fire
需要Texture &
。
在此上下文中,编译器不知道如何从 std::thread
构造函数解析所需的lasers::fire
重载,因为您是按值(没有引用(传递它的。
用 std::ref(t5)
包装t5
,以提示编译器您正在通过引用传递它。
std::thread th(&lasers::fire, &zxcv, std::ref(t5), ..., ...);
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误