W
W
William032018-12-06 18:00:39
C++ / C#
William03, 2018-12-06 18:00:39

Why error ERROR C2678: ...?

When compiling, Visual Studio throws an error:
error C2678: binary '<': no ​​operator found that takes a left operand of type 'const _Ty' (or there is no acceptable conversion).
What does this mean and how to fix it?
PS If these explanations help, here they are:
The Shape structure is a rectangle, the first two coordinates of which (x0, y0) are its lower left vertex, the second two
(x1, y1) are its upper right vertex. The vertices of the rectangle lie at the nodes of the integer lattice.
Structure Point - a point stores the coordinates of the point x0, y0.
The PointInShape function checks if a point lies within a rectangle. PointInArea
Functionchecks if the point lies in any of the rectangles, and also if we have visited it before. The shapes
vector is a set of rectangles . The used dictionary stores information whether point has been visited. The dist dictionary stores the distances to the vertices from the starting point. The input file contains the number of rectangles n and the next n lines contain the rectangles as the coordinates of the lower left and upper right vertices. The last two lines contain the coordinates of the start and end vertices. It is allowed to visit only those vertices that are located in at least one of the polygons. You can only walk


, as in chess (two cells up / down and one in the perpendicular direction).
It is necessary to print to the output file the distance length between the vertices, or -1 if there is no path between them.
Input example:
3
0 0 2 2
1 2 4 2
4 1 6 3
0 0
1 1
////////////////////////////////////// /////////////////////////////////////////////////// /////////////////////////////////////////////////// ////////////////

#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>
#include <fstream>
#include <queue>
using namespace std;


struct Shape {
  long x0, y0, x1, y1;
  explicit Shape(const long X0, const long Y0, const long X1, const long Y1) {
    x0 = X0;
    y0 = Y0;
    x1 = X1;
    y1 = Y1;
  }
};

struct Point {
  long x0, y0;
  explicit Point(const long X0,const long Y0) {
    x0 = X0;
    y0 = Y0;
  }
};

bool PointInShape(const Shape& shape,const Point& point) {
  if (point.x0 >= shape.x0 && point.x0 <= shape.x1 &&
    point.y0 >= shape.y0 && point.y0 <= shape.y1) return true;
  return false;
}

bool PointInArea(const vector <Shape>& sh, const map <Point, bool>& u,const Point& point) {
  bool InUsed = u.count(point);
  for (const Shape& s : sh) {
    if (PointInShape(s, point) && !InUsed) return true;
  }
  return false;
}

int main() {
  ifstream file_in("infile.txt");
  ofstream file_out("outfile.txt");
  long n;
  file_in >> n;
  long x1, y1, x2, y2;
  vector <Shape> shapes;
  map <Point, bool> used;
  map <Point, long> dist;
  for (int i = 0; i < n; ++i) {
    file_in >> x1 >> y1 >> x2 >> y2;
    shapes.push_back(Shape( x1, y1, x2, y2 ));
  }
  file_in >> x1 >> y1 >> x2 >> y2;
  const Point start(x1, y1);
  const Point end(x2, y2);
  queue <Point> points;
  points.push(start);
  dist[start] = 0;
  long length = -1;
  while (!points.empty()) {
    Point point = points.front();
    points.pop();
    for (const Point& p : {Point(point.x0+2, point.y0+1),
      Point(point.x0+2, point.y0-1),
      Point(point.x0+1, point.y0+2),
      Point(point.x0-1, point.y0-2)}) {
      if (p.x0 == end.x0 && p.y0 == end.y0) {
        length = dist[point] + 1;
        file_out << length;
        return 0;
      }
      else {
        if (PointInArea(shapes, used, p)) {
          points.push(p);
          dist[p] = dist[point] + 1;
        }
      }
    }
  }
  file_out << length;
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-12-06
@William03

for map<Point, bool> и map<Point, long>
need custom operator

bool operator<(const Point& p1, const Point& p2)
{
  return (p1.x0 < p2.x0) && (p1.y0 < p2.y0);
}

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question