포인터를 사용하지 않는 포인터를 사용한 경로 찾기 알고리즘의 일부를 리펙토링하려고합니다. 불행히도 나는 그 참조에 대해 knowledgable 아니에요. 오류가 발생합니다. Invalid operands to binary expression ('std::__1::reference_wrapper<Tile>' and 'const std::__1::reference_wrapper<Tile>')
reference_wrapper 벡터를 사용하는 방법
나는 또한 그 의미가 무엇인지 모릅니다. 내 코드는 아래에 있으며, 그 줄에서 오는 것을 수집 할 수 있습니다 : openList.erase(std::find(openList.begin(), openList.end(), current));
그러나이 문제를 해결하는 방법을 잘 모르겠습니다.
bool TileMap::tilesBetween(Tile& p_start, Tile& p_end)
{
std::vector<std::reference_wrapper<Tile>> openList;
std::vector<std::reference_wrapper<Tile>> closedList;
openList.push_back(p_start);
do
{
std::sort(openList.begin(), openList.end(), sortF());
Tile& current = openList[0];
closedList.push_back(current);
openList.erase(std::find(openList.begin(), openList.end(), current));
if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
{
return true;
}
std::vector<std::reference_wrapper<Tile>> adjacentTiles;
if (current.m_coordinates.x > 0)
{
adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x - 1)]);
}
if (current.m_coordinates.x < m_width)
{
adjacentTiles.push_back(m_tiles[current.m_coordinates.y * m_width + (current.m_coordinates.x + 1)]);
}
if (current.m_coordinates.y > 0)
{
adjacentTiles.push_back(m_tiles[(current.m_coordinates.y - 1) * m_width + current.m_coordinates.x]);
}
if (current.m_coordinates.y < m_height)
{
adjacentTiles.push_back(m_tiles[(current.m_coordinates.y + 1) * m_width + current.m_coordinates.x]);
}
for(auto t : adjacentTiles)
{
if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
{
continue;
}
if(std::find(openList.begin(), openList.end(), t) == closedList.end())
{
openList.push_back(t);
}
}
}
while(!openList.empty());
return false;
}
편집 : sortF
struct sortF
{
bool operator()(const Tile* p_a, const Tile* p_b) const
{
return p_a->f < p_b->f;
}
};
업데이트를 게시 : 제안에 따라, 나는 대신 참조 포인터를 사용하는 기능을 변경했습니다. 그것은 작동하는 것으로 보이지만 그것이 끝나기 전에 더 구현해야합니다.
bool TileMap::tilesBetween(Tile* p_start, Tile* p_end)
{
std::vector<Tile*> openList;
std::vector<Tile*> closedList;
std::cout << p_start << ", ";
openList.push_back(p_start);
do
{
std::sort(openList.begin(), openList.end(), sortF());
Tile* current = openList[0];
closedList.push_back(current);
openList.erase(std::find(openList.begin(), openList.end(), current));
if(std::find(closedList.begin(), closedList.end(), p_end) != closedList.end())
{
return true;
}
std::vector<Tile*> adjacentTiles;
if (current->m_coordinates.x > 0)
{
adjacentTiles.push_back(&m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x - 1)]);
}
if (current->m_coordinates.x < m_width)
{
std::cout << &m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x + 1)] << std::endl;
adjacentTiles.push_back(&m_tiles[current->m_coordinates.y * m_width + (current->m_coordinates.x + 1)]);
}
if (current->m_coordinates.y > 0)
{
adjacentTiles.push_back(&m_tiles[(current->m_coordinates.y - 1) * m_width + current->m_coordinates.x]);
}
if (current->m_coordinates.y < m_height)
{
adjacentTiles.push_back(&m_tiles[(current->m_coordinates.y + 1) * m_width + current->m_coordinates.x]);
}
for(auto t : adjacentTiles)
{
if(std::find(closedList.begin(), closedList.end(), t) != closedList.end())
{
continue;
}
if(std::find(openList.begin(), openList.end(), t) == openList.end())
{
openList.push_back(t);
}
}
}
while(!openList.empty());
return false;
}
무엇 라인 오류가에서 오는가 보여주기 위해? – Barry
@ 배리 - 나는'openList.erase (std :: find (openList.begin(), openList.end(), current));'줄로 좁혔다 고 생각한다. –
'Tile'에'operator =='가 구현되어 있습니까? – Barry