-2

I'm trying to make a c++ program where I can dynamically add points to a list, and then search for if those points are in the list.

I was trying originally to use sets, but am now trying to use vectors. My code below will run, but gets an error involving the search if statement.

#include <stdio.h>       /* printf */
#include <bits/stdc++.h> /* vector of strings */
#include <algorithm>
#include <vector>
using namespace std;

void solve_point(vector<vector<char>> &board)
{
    printf("solve_point\n");
    board[2][2] = 'c';
}

struct point
{
    int x;
    int y;
};

int main()
{

    vector<point> vec; 
    point point1{4, 1};
    point point1{8, 3};
    vec.push_back(point1);

    if (find(vec.begin(), vec.end(), point1) != vec.end())
    {   
        printf("point1 found in vec\n");
    }
    else
    {
        printf("point1 not found in vec\n");
    }

   }

This leads to some errors saying:

/usr/include/c++/7/bits/predefined_ops.h:241:17: error: no match for ‘operator==’ (operand types are ‘point’ and ‘const point’)
  { return *__it == _M_value; }

  ..and..

  /usr/include/c++/7/bits/predefined_ops.h:241:17: note:   ‘point’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
  { return *__it == _M_value; }

I encountered an error similar to this earlier when I was trying to insert a point into a set, is there something wrong with my struct point initialization? Thanks

0x499602D2
  • 87,005
  • 36
  • 149
  • 233
Martin
  • 616
  • 1
  • 12
  • 30
  • 1
    you need to define a `bool operator ==(const point& other) const` for you `point` class. if you define also an `operator` – Gian Paolo Mar 18 '20 at 21:26
  • 2
    Unrelated: The includes you use suggest that you are being sucked into [Cargo Cult Programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). In this particular instance, [educate yourself in what `bits/stdc++.h` is for](https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c), and then, before you start celebrating, [read a few reasons why you shouldn't directly include it](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Mar 18 '20 at 21:38

1 Answers1

1

You are experiencing two problems:

  1. std::find() needs a way of comparing two struct point instances. For this, it defaults to using point::operator==() which is not defined. You have to add this manually:
struct point
{
    int x;
    int y;

    bool operator==(const point& rhs) const {
        return x == rhs.x and y == rhs.y;   
    }
};

Alternatively you can use std::find_if() which allows you to supply a lambda. However, instances of a class like point will most likely need to be compared in a lot of places so defining operator==() is definitely a good idea.

  1. You're redeclaring point1. Rename it to for example point2.

Working example:

#include <bits/stdc++.h> /* vector of strings */
#include <algorithm>
#include <vector>
using namespace std;

void solve_point(vector<vector<char>> &board)
{
    printf("solve_point\n");
    board[2][2] = 'c';
}

struct point
{
    int x;
    int y;

    bool operator==(const point& rhs) const {
        return x == rhs.x and y == rhs.y;   
    }
};

int main()
{

    vector<point> vec; 
    point point1{4, 1};
    point point2{8, 3};
    vec.push_back(point1);

    if (find(vec.begin(), vec.end(), point1) != vec.end())
    {   
        printf("point1 found in vec\n");
    }
    else
    {
        printf("point1 not found in vec\n");
    }

   }
Joel Bodenmann
  • 1,776
  • 2
  • 11
  • 29
  • Answers should be as reusable as possible. Please replace `#include ` with the correct standard headers. – user4581301 Mar 18 '20 at 21:39
  • Comparison operators should be better implemented as non-member functions: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Ro-symmetric. – Jens Mar 18 '20 at 22:35