0

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.

  • 1
    Is it a c/p error or did you forget the `return` from that overload? – Nathan Wride Jul 11 '21 at 23:53
  • @NathanWride It was a copy/paste error. I fixed it. Thanks for pointing it out –  Jul 11 '21 at 23:54
  • 7
    Try: `bool operator==(const PhysicsVertex& other) const` – Eljay Jul 11 '21 at 23:57
  • @Eljay That made it work. Thanks. Can you explain why that works please? I know that the `const` qualifier tells the compiler that the method wont be modifying any of the members but why does that make this work? –  Jul 12 '21 at 00:00
  • 1
    @Eljay is right. If the `operator==` method is called from a const pointer or reference, the function itself must be const or it's not callable. – Mark Ransom Jul 12 '21 at 00:04
  • 2
    @user16038533 Because `FindIndex` is `const` the members appear `const`, including `objects.` The `.begin` member of a const container produces a `const_iterator` which is equivalent to a "pointer-to-const". Since `find` is working with `const_iterator` it can't call any non-`const` members of objects in the provided range. So it can't call a non-const `operator==`. – François Andrieux Jul 12 '21 at 00:08
  • 1
    It did not work with me except when I added `const` at the end of the line `bool operator==(const PhysicsVertex& other)`. It is also questionable to me how comparing the address of the class to the `other` 's would be enough to declare equality in `return this == &other;` better to compare key members. – Bilal Qandeel Jul 12 '21 at 00:15
  • I thought for sure this would be a dupe, but I couldn't find one. – Mark Ransom Jul 12 '21 at 00:15
  • 1
    @user16038533 Note that the error message complains about the **left**-hand operand. In the member function `operator==(const PhysicsVertex& other)`, the right-hand operand is `other`, while the left-hand operand is `*this`. Making changes to the type of `other` does not address the error; making changes to the type of `*this` does. – JaMiT Jul 12 '21 at 01:30
  • Looks like [No '==' operator found which takes a left-hand operand of const Type](https://stackoverflow.com/questions/41009150) is a duplicate, but if I vote it as such, this question will be closed as "caused by a typo". Not sure if that's the right thing to do at the moment. (Would it be different if a gold-badge like @MarkRansom did it?) – JaMiT Jul 12 '21 at 01:39
  • @JaMiT yes I did find that one, but I didn't think it made a good dupe. The code in the question wasn't good, and the self-answer never described why `const` was necessary - only that it seemed to work for them. It almost seemed like they stumbled upon the answer blindly. – Mark Ransom Jul 12 '21 at 02:04
  • So what someone should do is answer this one and mark the other one as a duplicate? – Jerry Jeremiah Jul 12 '21 at 02:49
  • @JaMiT "*Would it be different if a gold-badge ... did it?*" - yes. I took care of it. – Remy Lebeau Jul 12 '21 at 03:59

0 Answers0