Чисто виртуальный метод, называемый ошибкой

У меня есть следующие определения:

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;
}

Частичное определение для класса решателя Partitioning:

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; }
};

Извините, что вставил такой длинный фрагмент кода, но я работал над ним часами и продолжаю получать ошибку

pure virtual method called
terminate called without an active exception

Я не уверен, что не так. Кажется, сбой в функции computeInitialValues, где fprintf(stderr,"Begin partitioning\n"); расположен. Я пытался добавить заявления для печати в качестве крайней меры, но даже они ничего не печатают. Любые идеи приветствуются.

РЕДАКТИРОВАТЬ:
Итак, я изменил название Random на Ran, и оно начало работать. Я создавал этот экземпляр класса на лету как аргумент с новым Random(). Я думаю, он смешивался с другим конструктором или чем-то еще, чего я не знаю..

2 ответа

Существует другой тип ошибки, которая может привести к печати этого сообщения об ошибке.

Вы удалили объект, а затем пытаетесь позвонить по нему. Это неопределенное поведение, и на некоторых компиляторах, если вам повезет, это то, что вы увидите. Попробуйте запустить свой код с помощью valgrind.

http://tombarta.wordpress.com/2008/07/10/gcc-pure-virtual-method-called/

Другие вопросы по тегам