c++gecode

why does this simple Gecode example not compile?


I am trying to learn gecode and am trying to get the example found here to work.

// To use integer variables and constraints
#include <gecode/int.hh>
// To make modeling more comfortable
#include <gecode/minimodel.hh>  // To use search engines
#include <gecode/search.hh>
// To avoid typing Gecode:: all the time
using namespace Gecode;

class SendMoreMoney : public Space {
 protected:
  IntVarArray x;

 public:
  SendMoreMoney() : x(*this, 8, 0, 9) {
    IntVar s(x[0]), e(x[1]), n(x[2]), d(x[3]), m(x[4]), o(x[5]), r(x[6]),
        y(x[7]);
    rel(*this, s != 0);
    rel(*this, m != 0);
    distinct(*this, x);
    rel(*this,
        1000 * s + 100 * e + 10 * n + d + 1000 * m + 100 * o + 10 * r + e ==
            10000 * m + 1000 * o + 100 * n + 10 * e + y);
    branch(*this, x, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
  }
  SendMoreMoney(SendMoreMoney& s) : Space(s) { x.update(*this, s.x); }
  virtual Space* copy() { return new SendMoreMoney(*this); }
  void print() const { std::cout << x << std::endl; }
};

int main() {
  SendMoreMoney* m = new SendMoreMoney;
  DFS<SendMoreMoney> e(m);
  delete m;
  while (SendMoreMoney s = e.next()) {
    s->print();
    delete s;
  }
}

I end up with the following compilation errors.

error: no matching function for call to 'Gecode::IntVarArray::update(SendMoreMoney&, Gecode::IntVarArray&)'
   27 |             x.update(*this, s.x);
      |                                ^

and

error: invalid new-expression of abstract class type 'SendMoreMoney'
   30 |             return new SendMoreMoney(*this);
      |                

I do not understand where these come from. IntVarArray certainly has an update function whose first argument is a Space object and SendMoreMoney inherits from Space so what is the problem? This code is verbatim from the example I found so presumably it should work as-is.


Solution

  • e.next() returns a pointer of the cloned space (SendMoreMoney). You must use while (SendMoreMoney* s = e.next())