1

I have a Point class where I create each object by doing Point p(a, b) and I want to find whether the Point exists in a vector that I declared as vector<Point> vPoint.

A segment of my code is as follows;

Point p(a, b);
vector<Point>::iterator it = find(vPoint.begin(), vPoint.end(), p);
if(it != vPoint.end())
{
   // do something
}      

The error message I get is as follows; In file included from

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:846:22: error: 
      invalid operands to binary expression ('Point' and 'const Point')
        if (*__first == __value_)
            ~~~~~~~~ ^  ~~~~~~~~
segmentpoints.cpp:72:33: note: in instantiation of function template specialization 'std::__1::find<std::__1::__wrap_iter<Point
      *>, Point>' requested here
                        vector<Point>::iterator it = find(vPoint.begin(), vPoint.end(), p);
                                                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:403:1: note: 
      candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'Point'
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:574:1: note: 
      candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'Point'
operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:866:6: note: 
      candidate template ignored: could not match 'istreambuf_iterator<type-parameter-0-0, type-parameter-0-1>' against 'Point'
bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,

So I wonder whether this issue is due to not using pointers when creating objects/ iterator not supporting objects that are not pointed by pointers, or something else.

maregor
  • 777
  • 9
  • 32
  • 4
    The issue is due to the template code's inability to apply `operator ==` to instances of class `Point` that you defined. So.... define one? [**See this question**](https://stackoverflow.com/questions/4421706/operator-overloading) for ideas on different ways to provide operator overloads. – WhozCraig May 24 '15 at 05:32

1 Answers1

1

C++ doesn't automatically create comparison operator, so you must define what do you mean for two points to be equal. For example a reasonable implementation could be:

bool operator==(const Point& a, const Point& b) {
    return a.x == b.x && a.y == b.y;
}

Note also that if your code needs it you also have to implement operator != because C++ doesn't implement automatically != if given == or vice versa.

6502
  • 112,025
  • 15
  • 165
  • 265
  • If we had `Point*` instead, would be parameters be `const Point& *a, const Point& *b`? – maregor May 24 '15 at 06:08
  • 1
    @maregor: a `Point *` is a pointer and C++ already knows how to compare pointers for equality. Note however that it will check something different (i.e. two distinct point objects with the same coordinates will compare different). – 6502 May 24 '15 at 06:10