Код: Выделить всё
#include
#include
#include
#include
#include "game_object.hpp"
struct ByPosition {};
struct ByName {};
namespace bmi = boost::multi_index;
typedef boost::multi_index_container<
GameObject,
bmi::indexed_by<
bmi::random_access<
bmi::tag
>,
bmi::ordered_non_unique<
bmi::tag,
bmi::const_mem_fun
>
>
> GameObjectCollection;
class Game
{
//... various other code
private:
GameObjectCollection gameObjects_;
}
< /code>
Тогда внутри Game_object.hpp: < /p>
#pragma once
#include
#include "drawable.hpp"
class GameObject : public Drawable
{
public:
virtual ~GameObject() = default;
virtual void processEvent(const SDL_Event& event) {}
virtual void update(const float deltaTime) {}
const std::string& name() const { return name_; }
void setName(const std::string& name) { name_ = name; }
private:
std::string name_;
};
< /code>
Тогда Inside Game.cpp, где запускаются ошибки компилятора: < /p>
void Game::processEvent(const SDL_Event& event)
{
auto& index = gameObjects_.get();
for (auto it : index)
index.modify(it, [&event](GameObject& object) {
object.processEvent(event);
});
}
void Game::update()
{
auto& index = gameObjects_.get();
for (const auto& it : index)
index.modify(it, [&](GameObject& object) {
object.update(deltaTime_);
});
}
< /code>
И с этим повышением дает мне эти длинные ошибки компилятора: < /p>
game.cpp: In member function ‘void Game::processEvent(const SDL_Event&)’:
game.cpp:68:21: error: no matching function for call to ‘boost::multi_index::detail::random_access_index::modify(GameObject&, Game::processEvent(const SDL_Event&)::)’
68 | index.modify(it, [&event](GameObject& object) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 | object.processEvent(event);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 | });
| ~~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier) [with Modifier = Modifier; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
472 | bool modify(iterator position,Modifier mod)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: template argument deduction/substitution failed:
game.cpp:68:22: note: cannot convert ‘it’ (type ‘GameObject’) to type ‘boost::multi_index::detail::random_access_index::iterator’ {aka ‘boost::multi_index::detail::rnd_node_iterator’}
68 | index.modify(it, [&event](GameObject& object) {
| ^~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier, Rollback) [with Modifier = Modifier; Rollback = Rollback; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
493 | bool modify(iterator position,Modifier mod,Rollback back_)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: template argument deduction/substitution failed:
game.cpp:68:21: note: candidate expects 3 arguments, 2 provided
68 | index.modify(it, [&event](GameObject& object) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 | object.processEvent(event);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 | });
| ~~
game.cpp: In member function ‘void Game::update()’:
game.cpp:78:21: error: no matching function for call to ‘boost::multi_index::detail::random_access_index::modify(const GameObject&, Game::update()::)’
78 | index.modify(it, [&](GameObject& object) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 | object.update(deltaTime_);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
80 | });
| ~~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier) [with Modifier = Modifier; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
472 | bool modify(iterator position,Modifier mod)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: template argument deduction/substitution failed:
game.cpp:78:22: note: cannot convert ‘it’ (type ‘const GameObject’) to type ‘boost::multi_index::detail::random_access_index::iterator’ {aka ‘boost::multi_index::detail::rnd_node_iterator’}
78 | index.modify(it, [&](GameObject& object) {
| ^~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier, Rollback) [with Modifier = Modifier; Rollback = Rollback; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
493 | bool modify(iterator position,Modifier mod,Rollback back_)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: template argument deduction/substitution failed:
game.cpp:78:21: note: candidate expects 3 arguments, 2 provided
78 | index.modify(it, [&](GameObject& object) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 | object.update(deltaTime_);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
80 | });
| ~~
< /code>
Чтобы исправить его, я попробовал, например: < /p>
Заметив, что в нем говорится, что вместо 2 ожидались 3 аргумента, поэтому я проверил, и, по -видимому, вам, возможно, понадобится функция отката, что, по -видимому, какая -то другая документация предложила, что это опционально, но я пробовал это, я все равно не имел ничего, что можно было сделать, так что я пытался добавить пустые ламба, как это, например, на примере, на этом: на примере: на примере: на примере: на примере: на примере.void Game::update()
{
auto& index = gameObjects_.get();
for (const auto& it : index)
index.modify(it, [&](GameObject& object) {
object.update(deltaTime_);
}, [](GameObject& obj){});
}
< /code>
Но тогда это все еще дает мне ту же жалобу в сообщениях об ошибках компилятора, где я вижу это: < /p>
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: template argument deduction/substitution failed:
game.cpp:78:21: note: candidate expects 2 arguments, 3 provided
78 | index.modify(it, [&](GameObject& object) {
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 | object.update(deltaTime_);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
80 | }, [](GameObject& object) {});
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
< /code>
Я также пытался исправить все это, используя функторы вместо лямбда, поэтому в моей игре .hpp я добавил: < /p>
class ProcessEvent {
public:
ProcessEvent(const SDL_Event& event) : event_(event) {}
void operator()(GameObject& gameObject) { gameObject.processEvent(event_); }
private:
const SDL_Event& event_;
};
class Update {
public:
Update(const float delta) : delta_(delta) {}
void operator()(GameObject& gameObject) { gameObject.update(delta_); }
private:
const float& delta_;
};
< /code>
и изменил код в game.cpp на: < /p>
void Game::processEvent(const SDL_Event& event)
{
auto& index = gameObjects_.get();
for (auto it : index)
index.modify(it, ProcessEvent(event));
}
void Game::update()
{
auto& index = gameObjects_.get();
for (const auto& it : index)
index.modify(it, Update(deltaTime_));
}
< /code>
Но это просто дает мне эти сообщения об ошибках компилятора: < /p>
game.cpp: In member function ‘void Game::processEvent(const SDL_Event&)’:
game.cpp:68:21: error: no matching function for call to ‘boost::multi_index::detail::random_access_index::modify(GameObject&, ProcessEvent)’
68 | index.modify(it, ProcessEvent(event));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier) [with Modifier = Modifier; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
472 | bool modify(iterator position,Modifier mod)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: template argument deduction/substitution failed:
game.cpp:68:22: note: cannot convert ‘it’ (type ‘GameObject’) to type ‘boost::multi_index::detail::random_access_index::iterator’ {aka ‘boost::multi_index::detail::rnd_node_iterator’}
68 | index.modify(it, ProcessEvent(event));
| ^~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier, Rollback) [with Modifier = Modifier; Rollback = Rollback; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
493 | bool modify(iterator position,Modifier mod,Rollback back_)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: template argument deduction/substitution failed:
game.cpp:68:21: note: candidate expects 3 arguments, 2 provided
68 | index.modify(it, ProcessEvent(event));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
game.cpp: In member function ‘void Game::update()’:
game.cpp:76:21: error: no matching function for call to ‘boost::multi_index::detail::random_access_index::modify(const GameObject&, Update)’
76 | index.modify(it, Update(deltaTime_));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier) [with Modifier = Modifier; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
472 | bool modify(iterator position,Modifier mod)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:472:8: note: template argument deduction/substitution failed:
game.cpp:76:22: note: cannot convert ‘it’ (type ‘const GameObject’) to type ‘boost::multi_index::detail::random_access_index::iterator’ {aka ‘boost::multi_index::detail::rnd_node_iterator’}
76 | index.modify(it, Update(deltaTime_));
| ^~
In file included from game.hpp:6,
from game.cpp:3:
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: candidate: ‘template bool boost::multi_index::detail::random_access_index::modify(boost::multi_index::detail::random_access_index::iterator, Modifier, Rollback) [with Modifier = Modifier; Rollback = Rollback; SuperMeta = boost::multi_index::detail::nth_layer; TagList = boost::mpl::v_item]’
493 | bool modify(iterator position,Modifier mod,Rollback back_)
| ^~~~~~
/usr/include/boost/multi_index/random_access_index.hpp:493:8: note: template argument deduction/substitution failed:
game.cpp:76:21: note: candidate expects 3 arguments, 2 provided
76 | index.modify(it, Update(deltaTime_));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Надеюсь, кто -то здесь может помочь мне.>
Подробнее здесь: https://stackoverflow.com/questions/797 ... -when-loop
Мобильная версия