0
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<vector>
#include<map>
#include<cstdlib>
#include<ctime>

using namespace std;
int vertices = 5;
int grid_x = 5;
int grid_y = 5;
struct pos_in_grid
{
    int x_coordinate;
    int y_coordinate;
};
map<pos_in_grid,int> grid_with_placement;

int initial_placement();
int main()
{
    srand(time(NULL));
    initial_placement();
    return 0;
}

int initial_placement()
{
    for(int i = 1;i<=vertices;)
    {
        pos_in_grid pos_of_node;
        int j = rand()%grid_x;
        pos_of_node.x_coordinate = j;
        j = rand()%grid_y;
        pos_of_node.y_coordinate = j;
        if(grid_with_placement.find(pos_of_node) == grid_with_placement.end())
        {
            grid_with_placement[pos_of_node] = i;
            i++;
        }
        else
        {
            continue;
        }   

    }
    return 0;
}

I have been writing this function for an assignment and I the task I intend to achieve is that I want to add to my if the entry is not already present in the map. However, I run into 2 errors and the terminal shows a bunch of text that I find hard to understand.

My question is 2 fold. First, 1. How do I rectify this issue. 2. If the command line shows a ton of errors where should I start from to get my issues rectified.

Error message is as below:

/Library/Developer/CommandLineTools/usr/include/c++/v1/__functional_base:55:21: error: invalid operands to binary expression ('const pos_in_grid' and 'const pos_in_grid')
        {return __x < __y;}
  • 1
    Error messages usually start out with a line that begins with the word "Error:". That's the thing you need to focus on. It may be followed by more lines labeled "Note" or something like that. Those are giving you more details. Then there will be more "Error:" lines. Usually you shouldn't bother with any errors after the first one. The compiler made a guess at how to resolve the first one, and the ones after that just show that it guessed wrong. – Pete Becker Oct 05 '18 at 19:05
  • And now that you know to ignore everything after the first "Error", post the first "Error". – Pete Becker Oct 05 '18 at 19:07
  • Thanks @Pete Becker. You have been really helpful with my questions. I am new to STL, so I am having a hard time understanding some of the errors. – Rejoy Mathews Oct 05 '18 at 19:08
  • 1
    Since the error message doesn't give you a useful line number (it's for an internal file, not your file), the next thing you'll want to do is look at the extra details for that error message, since they'll usually help clue you in to where in particularly the compiler encountered the problem. You appear to be using Clang, so you should be able to find the specific line where it was emitted by taking a look at the notes beneath the error message itself. – Justin Time - Reinstate Monica Oct 05 '18 at 19:23
  • I think I have rectified the error. I did it by adding the following in my code bool operator<(const pos_in_grid& t1, const pos_in_grid& t2) { return (t1.x_coordinate < t2.x_coordinate); } This enables map to use a user defined structure as a key. Since map stores everything in a tree like structure, the < operator overloading enables map to do the same. – Rejoy Mathews Oct 05 '18 at 19:34

0 Answers0