lundi 4 juillet 2016

C++ std::find comparing a pointer to a reference

I am attempting to make a simple Graph system.

(I'm calling my nodes "Cells", but they're just basically nodes)

Here's my header:

class Cell {
 public:
  virtual ~Cell() {}
  Cell(int row, int column);
  std::tuple<int, int> getCoord() const;
  void link(Cell& adjacent, bool biDirectional = true);
  void unlink(Cell& adjacent, bool biDirectional = true);
  friend std::ostream& operator<<(std::ostream& out, const Cell& cell);
  bool operator==(const Cell& other) const;

 private:
  bool isLinked(const Cell& cell) const;
  int mRow, mColumn;
  std::vector<Cell*> links;
};

As you can see, I'm overloading the equivalency operator so I can compare cells by their coordinates. Here's the relevant methods in my .cpp file:

bool Cell::operator==(const Cell& other) const {
  return mRow == other.mRow && mColumn == other.mColumn;
}

bool Cell::isLinked(const Cell& cell) const {
  return std::end(links) ==
         std::find(std::begin(links), std::end(links),
                   [cell](Cell* cellPtr) { return (*cellPtr) == cell; });
}

As you can see, when I want to compare equivalency I check to see if the x,y coordinates are the same. In my Cell::isLinked method, I take the std::vector<Cell*> linksand pass them to std::find which then takes the object at the ptr and should then compare it (using the overloaded comparison operator from earlier) and return true or false.

Instead I'm getting this error when I try to build my project:

/Applications/http://ift.tt/29rfVVU: error: 
      invalid operands to binary expression ('Cell *' and 'const (lambda at Cell.cpp:51:20)')
        if (*__first == __value_)
            ~~~~~~~~ ^  ~~~~~~~~
Cell.cpp:50:15: note: in instantiation of function template specialization
      'std::__1::find<std::__1::__wrap_iter<Cell *const *>, (lambda at Cell.cpp:51:20)>' requested here
         std::find(std::begin(links), std::end(links),

Aucun commentaire:

Enregistrer un commentaire