I'm trying to implement Best First Search using C++ on VS2013. Below is the code.
//node for tree
struct Node
{
Node(std::string const& s, std::string const& p)
: state(s), path(p)
{}
const std::string state;
const std::string path;
};
//heuristic functor
struct ManhattanDistance
{
std::size_t operator()(std::string const& state, std::string const& goal)
{
std::size_t ret = 0;
for (int index = 0; index != goal.size(); ++index)
{
if ('0' == state[index])
continue;
auto digit = state[index] - '0';
ret += abs(index / 3 - digit / 3) + abs(index % 3 - digit % 3);// distance(row) plus distance(col)
}
return ret;
}
};
//functor to compare nodes using the heuristic function.
template<typename HeuristicFunc>
struct GreaterThan
{
explicit GreaterThan(HeuristicFunc h, std::string const& g = "012345678")
: goal(g), heuristic(h)
{}
bool operator()(Node const& lhs, Node const& rhs) const
{
return heuristic(lhs.state, goal) > heuristic(rhs.state, goal);
return true;
}
const std::string goal;
const HeuristicFunc heuristic;
};
When testing this code in Unit Test, compiler complained that :
Error 1 error C3848: expression having type 'const ai::search::ManhattanDistance' would lose some const-volatile qualifiers in order to call 'size_t ManhattanDistance::operator ()(const std::string &,const std::string &)'
How to understand this error? How to fix it?
Your method std::size_t ManhattanDistance::operator()(std::string const& state, std::string const& goal)
is not declared const
, yet you try to call it on a const ManhattanDistance
object. The compiler is correctly rejecting this ill-formed program.
Change the defining line to declare the method const
:
std::size_t operator()(std::string const& state, std::string const& goal) const
// ^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With