我有双重功能
double Grid::getAverageNeighborhoodFitnessEvaluation(int agentPositionX, int agentPositionY)
{
GetNeighbourhood(agentPositionX, agentPositionY,neighborhoodEvaluations);
int neighborscount = 0;
double fitnesssum = 0;
double neighborfitness;
double value;
for (size_t i = 0; i < neighborhoodEvaluations.size(); ++i)
{
if ((*(neighborhoodEvaluations.at(i))) != NULL)
{
neighborfitness = (*(neighborhoodEvaluations.at(i)))->GetFitness();
if(neighborfitness<0)
neighborfitness=0;
fitnesssum+=neighborfitness;
neighborscount++;
}
}
value = fitnesssum/neighborscount;
return value;
}GetNeighbourhood将定义类型(代理)的数组分配给neighborhoodEvaluations
*(neighborhoodEvaluations.at(i)))->GetFitness();返回一个双精度值,表示数组中该点的值。这些都是以前使用过的,没有任何问题。
从main调用时(其中RealX和RealY是两个整数)
int currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);总是有效的
double currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);导致分段故障
有没有人知道导致这种情况的可能性是什么,以及/或者整数可以获得什么值,而双精度值似乎不能?
到目前为止,我已经追踪到了我们的代理实现的错误
Agent.cpp
#include "Agent.h"
Agent::Agent(void)
{
m_age = 0;
m_fitness = -1;
}
Agent::~Agent(void)
{
}
int Agent::GetAge()
{
return m_age;
}
double Agent::GetFitness()
{
return m_fitness;
}
void Agent::IncreaseAge()
{
m_age++;
}
AgentType Agent::GetType()
{
return m_type;
}Agent.h
#ifndef AGENT_H
#define AGENT_H
enum AgentType { candidateSolution, cupid, reaper, breeder};
class Agent
{
public:
Agent(void);
virtual ~Agent(void);
double GetFitness();
int GetAge();
void IncreaseAge();
AgentType GetType();
virtual void RandomizeGenome() = 0;
protected:
double m_fitness;
AgentType m_type;
private:
int m_age;
};
#endif // !AGENT_H但似乎找不到确切的问题
发布于 2012-10-29 22:09:42
从您对gdb调试器答案的评论中,我看到您正在对空对象(Agent::GetFitness (this=0x0))调用GetFitness方法。这意味着neighborhoodEvaluations.at(i)将返回一个空指针。at()只检查越界,但是如果一开始放入数组的是空指针,at()就帮不上忙了。为了防止这种情况,你应该改变
if ((*(neighborhoodEvaluations.at(i))) != NULL)转到
if (neighborhoodEvaluations.at(i) != NULL)如果neighborhoodEvaluations不应该包含空指针,那么您将不得不追查getNeighborhood()为什么要将它们放在那里。也许您正在为您的点集边缘的元素寻找边界外邻居?
发布于 2012-10-29 20:59:56
使用本文http://www.cs.cmu.edu/~gilpin/tutorial/快速入门gdb调试器。然后告诉我们哪一条线产生了分段错误。
https://stackoverflow.com/questions/13121854
复制相似问题