2010-10-18 18 views
3

我得到了一个正常工作的C++面向对象程序。我决定修改它,通过添加一些polimorpysm来定义具有虚拟方法的类层次结构。当我调用虚拟方法时,它会产生错误分割错误,可能是因为我在对象中有垃圾。通过使用C++中的虚拟方法分段错误

这是呼叫和热身

GPUAntColony *colony; // Base class 
    GPUAntColonyConfiguration config; 
    set_config(config); 
    set_initial_pheromone(problem, config); 
    colony = (GPUAntColony *)new GPUSimpleAntColony(problem, config);//inhereted class 
    colony->run(); //Virtual method 

现在让我告诉你的基类

class GPUAntColony { 

private: 

    void reset_ants() { 
    for(unsigned int i=0; i<configuration_.number_of_ants;i++) { 
     ants_[i]= Util::random_number(problem_->number_of_vertices()); 
    } 
    } 

    void initialize_Pheromone(){ 
    for(unsigned int i=0; i<problem_->number_of_vertices()*problem_->number_of_vertices();i++) { 
     pheromones_[i]=(float)configuration_.initial_pheromone; 
    } 
    } 


    protected: 
    float * pheromones_; 
    float alpha_; 
    float beta_; 
    unsigned int iterations; 
    GPUAntColonyConfiguration::LocalSearchType local_search_type_; 
    GPUAntColonyConfiguration configuration_; 
    unsigned int * ants_; 
    GPUOptimizationProblem *problem_; 

    public: 


///Class Constructor for the Class GPU Ant Colony   
GPUAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config){ 

    iterations=4096; 
     problem_ = problem; // Including distance array 
    configuration_ = config; 
    ants_= (unsigned int*) malloc(config.number_of_ants*sizeof(unsigned int)); 
     pheromones_ = (float *) malloc(problem->number_of_vertices()*problem->number_of_vertices()*sizeof(float)); 
    alpha_ = config.alpha; 
    std::cout << "alpha_ " << alpha_ << std::endl; 
     beta_ = config.beta; 
     local_search_type_ = config.local_search; 
} 

    virtual void run(); 

    virtual ~GPUAntColony() { 
     delete problem_; 
     free(ants_); 
     free (pheromones_); 
    }; 


}; 

子类的定义

class GPUSimpleAntColony : public GPUAntColony{ 
public: 
    GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config); 
    void run(); 
}; 

最后的这种方法的实施

void GPUAntColony::run(){ 
    reset_ants(); 
    initialize_Pheromone(); 
} 


GPUSimpleAntColony::GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config):GPUAntColony(problem, config) { 
} 



void GPUSimpleAntColony::run() { 
GPUAntColony::run(); 
    antColonyGPULauncher(configuration_.number_of_ants, problem_->number_of_vertices(), problem_->get_distances(), pheromones_,ants_,alpha_, beta_, 
      configuration_.evaporation_rate, iterations, 0, 0, 0, 0, ACO_SIMPLE); 
} 

希望你能帮助我。

非常感谢提前。

+4

什么是转储的堆栈跟踪,当你拿到赛格故障?这可能与虚拟以及与'antColonyGPULauncher' – 2010-10-18 16:14:03

+0

有关的所有事情都没有关系是的,这就是我虽然在开始但antColonyGPUlauncher没有错,因为没有达到这行代码。分段错误是在调用虚拟方法run()时。 – Jose 2010-10-18 16:39:32

+0

@Jose:你可以添加堆栈跟踪吗? GDB到你的问题呢? – 2010-10-18 16:45:37

回答

0

在基类中运行纯虚拟或为其提供默认实现。

virtual void run() = 0; 

在子类中,declare run virtual。

virtual void run(); 

编辑:真正的问题是,基类run()不是纯虚拟的。正如下面的评论中所提到的,run()子类将自动变为虚拟的,但我认为明确地声明它是更清楚的。

比较http://codepad.org/6MlrH5Q4http://codepad.org/lBWaEefT

+0

链接器应该检测基类“run”是否缺少主体,并且C++将自动使所有具有相同签名虚拟的子'run'方法,即使它们没有被声明为这样。 – 2010-10-18 17:35:04

1

我注意到你不检查返回值与malloc分配内存。你有回到NULL的机会吗?特别是pheromones_似乎需要n * n个空格,并且您的示例代码不会给我们赋值n。

1

谢谢你们每一个伴侣,但它一如既往绝对是一件愚蠢的事情。我有一个影响执行的静态变量。现在可以正常使用,

谢谢很多

欢呼