纯虚方法称为错误

纯虚方法称为错误

本文介绍了纯虚方法称为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下定义:

class PartitioningMethod {
public:
  virtual void addConstraints(ConstraintManager& cm) = 0;
  virtual bool hasMoreConstraints() = 0;
  virtual void setQuery(const Query& q) = 0;
  virtual ~PartitioningMethod(){ }
};


class Random : public PartitioningMethod {
private:
  vector< ref<Expr> > constraints;
  vector< ref<Expr> >::iterator it;
  vector< ref<Expr> >::iterator end;
  int numConstraints;
  RNG theRNG;

public:
  void setQuery(const Query& q) {

    constraints.clear();

    //Set random number
    //srand ( unsigned ( time (NULL) ) * theRNG.getInt32() );
    srand ( theRNG.getInt32() );

    //Copy constraints
    copy(q.constraints.begin(),q.constraints.end(),std::back_inserter(constraints));

    //Shuffle Randomly
    std::random_shuffle(constraints.begin(),constraints.end(), p_myrandom);

    it = constraints.begin();
    end = constraints.end();
    numConstraints = constraints.size();
  }

  void addConstraints(ConstraintManager& cm) {
    int step = rand() % numConstraints + 1;
    while(step != 0) {
      cm.addConstraint(*it);
      ++it;
      --step;
      --numConstraints;
    }
  }

  bool hasMoreConstraints() {
    return it != end;
  }
};


bool PartitioningSolver::computeInitialValues(const Query& query,
                            const std::vector<const Array*> &objects,
                            std::vector< std::vector<unsigned char> > &values,
                            bool &hasSolution) {

  fprintf(stderr,"INIT\n");
  // If there are no constraints in the query
  if(query.constraints.size() == 0 || query.constraints.size() == 1)
    return solver->impl->computeInitialValues(query, objects, values, hasSolution);

  // If the number constraints in the query are > 0
  method->setQuery(query);

  ConstraintManager cm;
  ref<Expr> expr = query.expr;

  fprintf(stderr,"Begin partitioning\n");
  fprintf(stderr,"---------------------\n");

  while(method->hasMoreConstraints()){
    fprintf(stderr, "HERE");
    //Add Constraints
    method->addConstraints(cm);

    //Construct a query
    Query temp_query(cm,expr);

     ExprPPrinter::printQuery(std::cerr,temp_query.constraints,temp_query.expr);
     fprintf(stderr,"---------------------\n");

    //Query STP to check if satisfiable
    values.clear();

    if(!solver->impl->computeInitialValues(temp_query, objects, values, hasSolution))
      return false;

    //If not, return immediately (a win!)
    if(!hasSolution)
      return true;

    //If a solution is returned, check if the solution satisfies the entire set of constraints
    vector<const Array*> obj = objects;
    Assignment solution(obj, values);
    bool satisfiesAll = checkSolution(solution, query.constraints);

    //  fprintf(stderr,"Satisfies all: %i\n", satisfiesAll);

    // If it is successful, return the solution (a win again!),
    if(satisfiesAll)
      return true;

    // If not add more constraints (if there is more) and repeat
  }
  return true;
}

分区解算器类的部分定义:

A Partial definition for the Partitioning solver class:

class PartitioningSolver : public SolverImpl {
private:
  Solver*             solver;
  PartitioningMethod* method;
  bool checkSolution(Assignment& solution,  const ConstraintManager& constraints);
public:
  PartitioningSolver(Solver *s,  PartitioningMethod* pm) : solver(s), method(pm) { }
  ~PartitioningSolver() { delete solver; delete method; }
};

很抱歉,粘贴了这么长的代码段,但我已经工作了几个小时, eror

Sorry for pasting such a long snippet of code but I have been working on it for hours and keep getting the eror

pure virtual method called
terminate called without an active exception

我不知道是什么问题。它似乎失败在computeInitialValues函数其中 fprintf(stderr,开始分区\\\
);
位于。我尝试添加打印语句作为最后的手段,但即使他们不打印任何东西。任何想法是赞赏。

I am not sure what's wrong. It seems to fail in computeInitialValues function where fprintf(stderr,"Begin partitioning\n"); is located. I tried adding print statements as a last resort but even they don't print anything.. Any ideas is appreciated.

编辑:

好我把名字改为随机,并开始工作。我正在创建这个类实例作为一个参数与新的Random()我想它是与另一个构造函数或其他我不知道的混合。


Ok so I changed the name Random to Ran and it started to work. I was creating this class instance on the fly as an argument with new Random() I guess it was mixing up with another constructor or something else I dont know..

推荐答案

还有另一种类型的错误,可能会导致此错误消息的打印。

There's another type of bug, which can cause this error message to be printed.

您删除了对象,试图打电话给它。它是未定义的行为,在一些编译器,如果你幸运,这就是你会看到。尝试使用valgrind运行您的代码。

You deleted the object, and later you're trying to make a call on it. It's undefined behaviour, and on some compilers, if you're lucky, that's what you'll see. Try to run your code with valgrind.

这篇关于纯虚方法称为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:24