0

I have following 2 files implementation.h implementation.cpp

implementation.h is as follows:

#ifndef IMPLEMENTATION_H_
#define IMPLEMENTATION_H_

#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <array>
#include <vector>
#include <utility>
#include <queue>
#include <tuple>
#include <algorithm>
#include <cstdlib>

using namespace std;

struct SimpleGraph {
  std::map<char, std::vector<char> > edges;

  std::vector<char> neighbors(char id);
};

struct GridLocation {
  int x, y;

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

  bool operator != (const GridLocation& a, const GridLocation& b) {
    return !(a == b);
  }

  bool operator < (const GridLocation& a, const GridLocation& b) {
    return std::tie(a.x, a.y) < std::tie(b.x, b.y);
  }

  std::basic_iostream<char>::basic_ostream& GridLocation::operator<<(std::basic_iostream<char>::basic_ostream& out, const GridLocation& loc) {
    out << '(' << loc.x << ',' << loc.y << ')';
    return out;
  }

};

struct SquareGrid{
  static std::array<GridLocation, 4> DIRS;

  int width, height;

  std::set<GridLocation> walls;

  SquareGrid(int width_, int height_): width(width_), height(height_) {};

  bool in_bounds(GridLocation id);

  bool passable(GridLocation id);


  std::vector<GridLocation> neighbors(GridLocation id);

};




#endif /* IMPLEMENTATION_H_ */

I have following structs defined: SimpleGraph, GridLocation, SquareGrid

I have defined operator overloading for GridLocation struct

implementation.cpp is as follows:

#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <array>
#include <vector>
#include <utility>
#include <queue>
#include <tuple>
#include <algorithm>
#include <cstdlib>
#include "implementation.h"

std::vector<char> SimpleGraph::neighbors(char id) {
    return edges[id];
}

bool SquareGrid::in_bounds(GridLocation id) {
    return 0 <= id.x && id.x < width
        && 0 <= id.y && id.y < height;
};

bool SquareGrid::passable(GridLocation id) {
    return walls.find(id) == walls.end();  // get 2 errors here **
};

std::vector<GridLocation> SquareGrid::neighbors(GridLocation id) {
    std::vector<GridLocation> results;

    for (GridLocation dir : DIRS) {
      GridLocation next{id.x + dir.x, id.y + dir.y};
      if (in_bounds(next) && passable(next)) {
        results.push_back(next); // get 1 error here -*-
      }
    }

    if ((id.x + id.y) % 2 == 0) {
      // aesthetic improvement on square grids
      std::reverse(results.begin(), results.end());
    }

    return results;
}

Errors for ** are

1) in instantiation of member function 'std::__1::set<GridLocation, std::__1::less<GridLocation>, std::__1::allocator<GridLocation> >::find' requested here
2) Invalid arguments '
Candidates are:
std::__1::__tree_const_iterator<GridLocation,std::__1::__tree_node<GridLocation,void *> *,long int> find(const GridLocation &)
'

Errors for -*- are:

1) Invalid arguments '
Candidates are:
void push_back(const GridLocation &)
void push_back(GridLocation &&)
'

I am using Eclipse CDT on mac osx with c++11.

Any help/pointers figuring out the errors above will be appreciated

mg03
  • 667
  • 2
  • 9
  • 14
  • For `operator==` to be a member, one of the parameters must be `this` (in other words, you can only provide one parameter). If you want to be able to compare any `GridLocation` to any other `GridLocation` as opposed to `this`, declare `operator==` to be a friend. – user4581301 Sep 25 '18 at 00:05
  • Same thing will happen with `operator !=` and `operator <`. `operator<<` should always be a stand-alone function (a Free Function). – user4581301 Sep 25 '18 at 00:07
  • Much wisdom on operator overloading can be found at [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Sep 25 '18 at 00:08
  • Recomendation: Abandon the Problems tab in favour of the Console tab. The Build output in the console is complete and cuts and pastes into a stack overflow question much more easily. – user4581301 Sep 25 '18 at 00:17

0 Answers0