0

I have a vector of structs. I need to check if the struct is or is not in the vector. The entire struct, not any specific member. It throws me this error upon compile time:

binary '==' : no operator found which takes a left-hand operand of type 'NavigationNode'
(or there is no acceptable conversion)

My struct:

 struct NavigationNode{ 
    int x, y; //their x and y position on the grid
    float f, g, h;
    int parentGCost;
    int value;
};

NavigationNode currentNode;

The vector

vector<NavigationNode> openList;

My find:

 if (find(closedList.begin(), closedList.end(), currentNode) == closedList.end() )
 {
 }
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
meWantToLearn
  • 1,691
  • 2
  • 25
  • 45

3 Answers3

5

You need to overload operator==.

As global function:

bool operator==( const NavigationNode& lhs, const NavigationNode& rhs )
{
    // compare lhs and rhs
}

Or as member function:

bool operator==( const NavigationNode& other ) const
{
    // compare this & other
}
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
2

You will have to write an equality operator for your custom type. Assuming all variables have to be the same for two NavigationNode objects to be the same, it should look something like this:

bool floatEqual(float a, float b)
{
     // adapt this comparison to fit your use-case - see the notes below
     static const int EPSILON = 0.00001;    // arbitrarily chosen - needs to be adapted to the occuring values!
     return std::abs(a – b) <= EPSILON;
}

bool operator==(NavigationNode const & a, NavigationNode const & b)
{ 
    return a.x == b.x &&
        a.y == b.y &&
        floatEqual(a.f, b.f) &&
        floatEqual(a.g, b.g) &&
        floatEqual(a.h, b.h) &&
        a.parentGCost == b.parentGCost &&
        a.value == b.value;
}

Even if you could also do it as a member function of NavigationNode, the recommended way is to implement the operator== as a free function (that way, both parameters can take advantage of any possible implicit conversions).

Note on float comparison: Due to how floating point numbers are represented, it is not a trivial task to compare them. Just checking for equality might not give the desired results. See e.g. this question for details: What is the most effective way for float and double comparison?

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
  • `a.x = b.x` is an assignment – qehgt Dec 20 '13 at 13:51
  • 1
    Comparing floating point for equality can be tricky, unless it was a copy of the original, the floating point numbers might be mathematically equivalent (obtained by operations that mathematically end up yielding the same value) and yet compare as different – David Rodríguez - dribeas Dec 20 '13 at 14:07
  • @DavidRodríguez-dribeas - nice point, I was just writing the same. – Kiril Kirov Dec 20 '13 at 14:07
  • @nyarlathotep - usually, `fabs`, combined with subtraction and comparing to `e`psilon (very small positive floating point number, like `.0001` or something; depending on the necessary precision) is a nice approach. – Kiril Kirov Dec 20 '13 at 14:13
0

You need to overload the comparison operator. If your intention of "==" is "are each of the values contained in my struct equal to the corresponding members in this other struct" then you can write that.

bool operator==(const NavigationNode& lhs, const NavigationNode& rhs)
{
    return /* compare each member in here */
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218