I need to write a method that takes in an object of type PhysicsVertex
and return which index position it is in, in a std::vector. I followed the accepted answer to this question.
Here are the necessary bits copied here from my code just enough to make it stand-alone.
#include <iostream>
#include <vector>
#include <algorithm>
class PhysicsVertex
{
public:
bool operator==(const PhysicsVertex& other)
{
return this == &other;
}
};
class ViewPortObjectContainer
{
private:
std::vector<PhysicsVertex> objects;
public:
void Push(const PhysicsVertex& object) noexcept
{
objects.push_back(object);
}
const std::ptrdiff_t FindIndex(const PhysicsVertex& pv) const noexcept
{
return std::distance(
objects.begin(), std::find(objects.begin(), objects.end(), pv)
);
}
};
class ViewPort
{
public:
ViewPortObjectContainer container;
};
int main()
{
PhysicsVertex vertex;
ViewPort viewPort;
viewPort.container.Push(vertex);
viewPort.container.Push({});
viewPort.container.Push({});
std::cout << viewPort.container.FindIndex(vertex);
return 0;
}
This gives an error message saying
binary '==': no operator found which takes a left-hand operand of type 'const PhysicsVertex' (or there is no acceptable conversion)
I was using the same ==
overload for a method that removes the given PhysicsVertex
using the remove-erase idiom and that was working fine.
The error message is saying ...type 'const PhysicsVertex'...
so I tried to add an additional overload with signature of bool operator==(const PhysicsVertex other)
(notice the missing &) but the I got a different error message saying operator== is ambigious
so I don't know what to do.