我正在尝试学习gecode,并尝试让示例发现的here工作。
// 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;
}
}我最终得到了以下编译错误。
error: no matching function for call to 'Gecode::IntVarArray::update(SendMoreMoney&, Gecode::IntVarArray&)'
27 | x.update(*this, s.x);
| ^和
error: invalid new-expression of abstract class type 'SendMoreMoney'
30 | return new SendMoreMoney(*this);
| 我不明白这些是从哪里来的。IntVarArray当然有一个update函数,它的第一个参数是一个Space对象,而SendMoreMoney继承自Space,那么有什么问题呢?这段代码与我发现的示例完全相同,因此它应该可以按原样工作。
发布于 2021-08-26 20:25:29
e.next()返回克隆空间的指针(SendMoreMoney)。您必须使用while (SendMoreMoney* s = e.next())
https://stackoverflow.com/questions/64723904
复制相似问题