我有双重功能
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 将一个定义类型(Agent)的数组分配给neighborhoodEvaluations
*(neighborhoodEvaluations.at(i)))->GetFitness();
返回表示数组中该点的值的双精度值。这些都是以前使用过的,没有问题。
从我的 main 调用时(其中 RealX 和 RealY 是两个整数)
int currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);
总是有效
double currentFitness = getAverageNeighborhoodFitnessEvaluation(RealX,RealY);
导致分段错误
有谁知道什么可能性会导致这种情况和/或 int 可以取什么值但 double 似乎不能?
到目前为止,我已经将错误追溯到我们的代理实现
代理.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;
}
代理.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
虽然似乎无法找到确切的问题