gpt4 book ai didi

C++ std::find lambda 表达式

转载 作者:太空狗 更新时间:2023-10-29 23:28:08 26 4
gpt4 key购买 nike

对于我来说,我无法让这段代码正常工作。在 XNA 框架弃用后,我试图将我的代码从 C# 转换为 C++,但是一个顽固的方法不想转换。在 C# 中是:

public Tile GetTileAtPosition(bool screenOrGame, Vector2 position)
{

if (screenOrGame)
{
return Array.Find(tileList, tile => tile.Position == position / 24);
}
else
{
return Array.Find(tileList, tile => tile.Position == position);
}
}

在 C++ 中,我尝试使用的代码是:

Tile Level::GetTileAtPosition(bool screenOrGame, sf::Vector2f position)
{
vector<Tile>::iterator it;

if (screenOrGame)
{

it = find(tileList.begin(), tileList.end(), [position](const Tile &t) { return t.GetPosition() == sf::Vector2f(position.x / 24, position.y / 24); });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);

}

else
{

it = find(tileList.begin(), tileList.end(), [position](const Tile& t) { return t.GetPosition() == position; });
return Tile(it->GetID(), it->GetPosition().x, it->GetPosition().y);

}

}

在 C++ 赋值行 (it = ...) 我遇到了一个我无法找出原因或解决方案的棘手错误。它返回:

error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const Blobby::Level::GetTileAtPosition::<lambda_29eb981cd341d9c05d39c4654bc470b9>' (or there is no acceptable conversion)    c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186

有什么方法可以修复这个错误,或者有更好/更实用的方法来在 C++ 中实现该方法吗?

最佳答案

在 C++ 中,采用比较器的版本有时会以 _if 为后缀。 std::find 就是这种情况。 std::find 采用要查找的元素,而 std::find_if 采用实现相等性的比较器。该错误仅表示它无法找到等同于 lambda 的 Tile 的匹配项。

关于C++ std::find lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16782870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com