meW*_*arn 0 c++ struct stdvector
我有一个结构矢量.我需要检查结构是否在向量中.整个结构,而不是任何特定的成员.它在编译时抛出了这个错误:
binary '==' : no operator found which takes a left-hand operand of type 'NavigationNode'
(or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)
我的结构:
struct NavigationNode{
int x, y; //their x and y position on the grid
float f, g, h;
int parentGCost;
int value;
};
NavigationNode currentNode;
Run Code Online (Sandbox Code Playgroud)
矢量
vector<NavigationNode> openList;
Run Code Online (Sandbox Code Playgroud)
我的发现:
if (find(closedList.begin(), closedList.end(), currentNode) == closedList.end() )
{
}
Run Code Online (Sandbox Code Playgroud)
你需要超载operator==.
作为全球功能:
bool operator==( const NavigationNode& lhs, const NavigationNode& rhs )
{
// compare lhs and rhs
}
Run Code Online (Sandbox Code Playgroud)
或作为会员功能:
bool operator==( const NavigationNode& other ) const
{
// compare this & other
}
Run Code Online (Sandbox Code Playgroud)