I have a fairly large C ++ program, including the Character class. In "Character.h", struct CharacterSettings is declared first, and then the Character class (including their constructors).
The character has (among others) CharacterSettings * and Point pos settings. CharacterSettings has a privileged value of Point.
This works great.
However, when I add some public variable to Character, the program crashes when I call this:
drawLine(character.pos, character.pos+character.settings->preferredVelocity, character.radius/3.0, 128, 80, 0);
The program crashes on this line:
Point operator + (const Point &p2) const
{ return Point(x + p2.x, y + p2.y); }
I assume he is trying to do character.pos + character.settings-> preferredVelocity. The error message I get is
Unhandled exception at 0x004bc4fc in ECMCrowdSimulation.exe: 0xC0000005: Access violation reading location 0x7f80000f.
, p2.x p2.y undefined. . , , , ! !
: , , Character.h!
#pragma once
#include "../ECM/GraphComponents/CMNode.h"
#include "VectorOperation.h"
#include "IndicativeRoute.h"
#include "float.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
using std::vector;
#include <queue>
using std::queue;
#define CHARACTER_RELAXATIONTIME 0.5f
typedef vector<CMNode>::iterator node_ptr;
class Corridor;
class CMMResult;
struct CMEdge;
class CMMInterface;
class MicroInterface;
class CMMSceneTransfer;
struct CharacterSettings
{
private:
bool index_bb_initialized, index_bb_cp_initialized, index_ir_circle_initialized, index_ir_circle_mu_initialized;
bool index_2nd_ir_circle_initialized, index_2nd_ir_circle_mu_initialized;
public:
int id;
Point preferredVelocity;
Point newVelocity;
float total_max_speed;
float max_speed;
float min_desired_speed;
Point lastAttractionPoint;
CMMInterface* cmmImplementation;
float sidePreference;
float sidePreferenceNoise;
float preferred_clearance;
MicroInterface* microImplementation;
short micro_maxNrNeighbours;
float micro_personalSpaceRadius;
node_ptr index_bb;
node_ptr index_bb_cp;
curve_ptr index_ir_circle;
float index_ir_circle_mu;
friend Character;
CharacterSettings(int _id,
float _total_max_speed, float _min_desired_speed,
CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance,
MicroInterface* _microImplementation) :
id(_id), total_max_speed(_total_max_speed), min_desired_speed(_min_desired_speed),
cmmImplementation(_cmmImplementation), sidePreference(_sidePreference), sidePreferenceNoise(_sidePreferenceNoise), preferred_clearance(_clearance),
microImplementation(_microImplementation)
{
newVelocity = Point(0, 0);
max_speed = total_max_speed;
preferredVelocity = Point(0, 0);
index_bb_initialized = false;
index_bb_cp_initialized = false;
index_ir_circle_initialized = false;
index_ir_circle_mu_initialized = false;
micro_maxNrNeighbours = 5;
micro_personalSpaceRadius = 0.0f;
}
};
class Character
{
public:
Point pos;
float radius;
Point prevPos;
int i;
Point goalPos;
float goalRadius;
bool reachedGoal;
bool freeze;
bool freezeNotified;
bool reachedDestSet;
Point velocity;
Point retraction, cp;
CharacterSettings * settings;
public:
Character(int _id, Point &_pos, float _radius,
float _total_max_speed, float _min_desired_speed,
CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance,
MicroInterface* _microImplementation) :
pos(_pos), radius(_radius)
{
settings = new CharacterSettings(_id, _total_max_speed, _min_desired_speed,
_cmmImplementation, _sidePreference, _sidePreferenceNoise, _clearance, _microImplementation);
velocity = Point(0, 0);
prevPos=_pos;
reachedGoal = true;
freeze = false;
freezeNotified = false;
reachedDestSet = false;
}
void removeSettings();
inline void integrateEuler(const Point &acc, float dtSim)
{
settings->newVelocity = velocity + dtSim * acc;
trim(settings->newVelocity, settings->max_speed);
}
inline void updatePos(float dtSim)
{
prevPos=pos;
velocity = settings->newVelocity;
pos += dtSim * velocity;
if(!reachedGoal
&& settings->lastAttractionPoint == goalPos
&& distSqr(pos, goalPos) < 0.25)
{
reachedGoal = true;
}
}
void resetIndices();
node_ptr &getIndex_bb(Corridor &corridor);
node_ptr &getIndex_bb_cp(Corridor &corridor);
curve_ptr &getIndex_ir_circle(IndicativeRoute &ir);
float &getIndex_ir_circle_mu();
Point &getRetraction() { return retraction; }
Point &getClosestPoint() { return cp; }
float getEdgeCost(const CMEdge& edge, float activeFraction);
float getArea() const;
};
, , "int i".