C++ 称为error的纯虚拟方法

C++ 称为error的纯虚拟方法,c++,exception,virtual-functions,C++,Exception,Virtual Functions,我有以下定义: class PartitioningMethod { public: virtual void addConstraints(ConstraintManager& cm) = 0; virtual bool hasMoreConstraints() = 0; virtual void setQuery(const Query& q) = 0; virtual ~PartitioningMethod(){ } }; class Random :

我有以下定义:

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;
}
很抱歉粘贴了这么长的一段代码,但我已经花了几个小时在上面,并且一直在获取eror

pure virtual method called
terminate called without an active exception
我不知道怎么了。它似乎在computeInitialValues函数中失败,其中
fprintf(stderr,“Begin partitioning\n”)位于。我尝试添加打印语句作为最后手段,但即使它们也不会打印任何内容。。欢迎提出任何意见

编辑:

好的,我把名字改为Random,它开始工作了。我正在用new Random()动态创建这个类实例作为参数,我猜它与另一个构造函数或其他我不知道的东西混在一起了。

您在一些代码中从一个构造函数调用了一个纯虚函数,您没有包含这些代码供我们查看


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

您删除了该对象,然后尝试对其进行调用。这是未定义的行为,在某些编译器上,如果幸运的话,这就是您将看到的。尝试使用valgrind运行代码


是的,为什么会发生这种情况?有什么想法吗?您尝试过使用调试器吗?由于代码库的各种限制,我无法使用调试器。我基本上不能用-g选项为gdb或其他语言编译它tools@Cemre:到底是什么限制阻止您使用带有调试信息的调试器/编译?我真的很好奇。此外:我想说的是,您的错误很可能是您在某处调用了纯虚拟方法(这就是错误消息所说的),那么您为什么需要我们查找它?删除代码直到它开始工作。我尝试使用valgrind,但出于某种原因,它没有打印此错误的错误消息。。我只得到崩溃输出,就这样
pure virtual method called
terminate called without an active exception