首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个简单的Gecode示例不能编译?

为什么这个简单的Gecode示例不能编译?
EN

Stack Overflow用户
提问于 2020-11-07 10:38:40
回答 1查看 59关注 0票数 0

我正在尝试学习gecode,并尝试让示例发现的here工作。

代码语言:javascript
复制
// 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;
  }
}

我最终得到了以下编译错误。

代码语言:javascript
复制
error: no matching function for call to 'Gecode::IntVarArray::update(SendMoreMoney&, Gecode::IntVarArray&)'
   27 |             x.update(*this, s.x);
      |                                ^

代码语言:javascript
复制
error: invalid new-expression of abstract class type 'SendMoreMoney'
   30 |             return new SendMoreMoney(*this);
      |                

我不明白这些是从哪里来的。IntVarArray当然有一个update函数,它的第一个参数是一个Space对象,而SendMoreMoney继承自Space,那么有什么问题呢?这段代码与我发现的示例完全相同,因此它应该可以按原样工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-26 20:25:29

e.next()返回克隆空间的指针(SendMoreMoney)。您必须使用while (SendMoreMoney* s = e.next())

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64723904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档