我有双重功能
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();
返回一个double,表示数组中该点的值。这些都已经使用过,没有任何问题。
从我的main调用时(RealX和RealY是两个整数)
int currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);
始终有效
double currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);
导致分段错误
有谁知道可能会导致这种可能性和/或int可以采用什么值,但似乎不能达到双倍?
到目前为止,我已将错误追溯到我们的代理实施
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
似乎无法找到确切的问题
答案 0 :(得分:0)
使用本文http://www.cs.cmu.edu/~gilpin/tutorial/快速开始使用gdb调试器。然后告诉我们哪条线产生分段故障。
答案 1 :(得分:0)
根据您对gdb调试器答案的评论,我看到您在空对象(GetFitness
)上调用Agent::GetFitness (this=0x0)
方法。这意味着neighborhoodEvaluations.at(i)
返回一个空指针。 at()只检查越界,但如果开始放入数组的是空指针,at()
将无法帮助你。为了防范这一点,你应该改变
if ((*(neighborhoodEvaluations.at(i))) != NULL)
到
if (neighborhoodEvaluations.at(i) != NULL)
如果neighborEvaluations不应包含空指针,则必须追踪getNeighborhood()
将它们放在那里的原因。也许你正在为你的点集边缘的元素寻找越界的邻居?