添加公共变量时崩溃 我有一个相当大的C++程序,包括一个“字符”类。在“Character.h”中,首先声明struct CharacterSettings,然后声明类字符(包括它们的构造函数)
角色具有(除其他外)CharacterSettings*设置和点位置。 CharacterSettings有一个点preferredVelocity 这个很好用 但是,当我向Character添加任何公共变量时,当我调用以下命令时,程序崩溃:添加公共变量时崩溃 我有一个相当大的C++程序,包括一个“字符”类。在“Character.h”中,首先声明struct CharacterSettings,然后声明类字符(包括它们的构造函数),c++,variables,crash,C++,Variables,Crash,角色具有(除其他外)CharacterSettings*设置和点位置。 CharacterSettings有一个点preferredVelocity 这个很好用 但是,当我向Character添加任何公共变量时,当我调用以下命令时,程序崩溃: drawLine(character.pos, character.pos+character.settings->preferredVelocity, character.radius/3.0, 128, 80, 0); 程序在此行崩溃: Poi
drawLine(character.pos, character.pos+character.settings->preferredVelocity, character.radius/3.0, 128, 80, 0);
程序在此行崩溃:
Point operator + (const Point &p2) const
{ return Point(x + p2.x, y + p2.y); }
我假设它正在尝试执行character.pos+character.settings->preferredVelocity。我收到的错误消息是
Unhandled exception at 0x004bc4fc in ECMCrowdSimulation.exe: 0xC0000005: Access violation reading location 0x7f80000f.
当我看它时,p2.x和p2.y是未定义的。如果没有额外的变量,它们就不是。我完全不知道发生了什么,甚至不知道如何开始调试,也不知道您需要什么信息来帮助我!任何帮助都将不胜感激
编辑:这至少是Character.h文件
#pragma once
/*
* ECM navigation mesh / crowd simulation software
* (c) Roland Geraerts and Wouter van Toll, Utrecht University.
* ---
* Character: A single moving character in the simulation.
*/
#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:
// --- Unique identifier within the simulation
int id;
// --- Velocity and speed
Point preferredVelocity;// Newly computed velocity *before* local collision avoidance
Point newVelocity; // Newly computed velocity (+ collision avoidance), to be applied in the "next" simulation step
float total_max_speed; // Maximum possible speed throughout the entire simulation
float max_speed; // Maximum speed at this point in time
float min_desired_speed;// Minimum speed that the character tries to reach when it is not standing still
Point lastAttractionPoint;
// --- IRM parameters
CMMInterface* cmmImplementation; // the type of indicative route to follow within the corridor, e.g. "shortest path" or "weighted side".
// Only used in WEIGHTED_SIDE:
float sidePreference; // bias to following a certain "side" of the corridor. Must be between -1 (left) and 1 (right).
float sidePreferenceNoise; // extra noise factor that will be added to sidePreference at each route element.
// Used in WEIGHTED_SIDE and SHORTEST_PATH
float preferred_clearance; // Distance (m) by which the agent prefers to stay away from obstacles.
// --- Micro simulation model (e.g. for collision avoidance between characters)
MicroInterface* microImplementation;// the local model to use
short micro_maxNrNeighbours; // the number of neighbours to check in the local model
float micro_personalSpaceRadius; // radius of the personal space (m), on top of the character's physical radius.
// Entering this disk (radius + personalSpace) is seen as a 'collision'.
// --- Corridor/Path pointers
node_ptr index_bb; // point on backbone path (used for computing attraction force)
node_ptr index_bb_cp; // point on the backbone path(used for computing the closest point)
curve_ptr index_ir_circle; // index to last point on the indicative route that intersects with a circle
float index_ir_circle_mu; // factor wrt to point on the indicative route that intersects with a circle
friend Character; // only the Character class can look into private members (WvT: ugly C++ practice, but it does work)
CharacterSettings(int _id,
// speed
float _total_max_speed, float _min_desired_speed,
// type of indicative route
CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance,
// type of micro simulation model
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)
{
// velocities
newVelocity = Point(0, 0);
max_speed = total_max_speed;
preferredVelocity = Point(0, 0);
// corridor/IRM pointers
index_bb_initialized = false;
index_bb_cp_initialized = false;
index_ir_circle_initialized = false;
index_ir_circle_mu_initialized = false;
// default micro settings
micro_maxNrNeighbours = 5; // default for Karamouzas 2010: 5
micro_personalSpaceRadius = 0.0f; // default for Karamouzas 2010: 0.5
}
};
class Character
{
public:
Point pos;
float radius;
Point prevPos;
int i; //The thing that is pretending to be the culprit, without this, it works fine.
// goal data
Point goalPos;
float goalRadius;
// status flags
bool reachedGoal;
bool freeze; // whether or not the character is temporarily frozen
bool freezeNotified;
bool reachedDestSet;
Point velocity; // last used velocity
// corridor/path pointers
Point retraction, cp;
//Contains more detailed settings of agent
CharacterSettings * settings;
public:
// --- constructor
Character(int _id, Point &_pos, float _radius,
// speeds
float _total_max_speed, float _min_desired_speed,
// type of indicative route
CMMInterface* _cmmImplementation, float _sidePreference, float _sidePreferenceNoise, float _clearance,
// type of micro simulation model
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;
//isProxy = false;
}
// --- destructor
void removeSettings();
// computing the new actual velocity through an acceleration vector: Euler integration
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;
// update velocity
velocity = settings->newVelocity;
// update position
pos += dtSim * velocity;
// if the character is close to its goal, it should stop moving
if(!reachedGoal // goal was not already reached
&& settings->lastAttractionPoint == goalPos
&& distSqr(pos, goalPos) < 0.25)//goalRadius)
{
reachedGoal = true;
// (do not reset the velocity, so that we can keep the last walking direction)
}
}
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; }
// computing the cost of some edge (in A*), by using personal preferences
float getEdgeCost(const CMEdge& edge, float activeFraction);
// computing the character's area, based on its radius
float getArea() const;
};
#pragma一次
/*
*ECM导航网格/群组模拟软件
*(c)乌得勒支大学罗兰·杰拉尔斯和沃特·范托尔。
* ---
*角色:模拟中的单个移动角色。
*/
#包括“./ECM/GraphComponents/CMNode.h”
#包括“VectorOperation.h”
#包括“IndicativeRoute.h”
#包括“float.h”
#定义使用数学定义
#包括
#包括
使用std::vector;
#包括
使用std::queue;
#定义字符_松弛时间0.5f
typedef向量::迭代器节点_ptr;
班级走廊;
班级成绩;
结构CMEdge;
类接口;
类微接口;
类别CMMScenetTransfer;
结构字符设置
{
私人:
布尔索引已初始化,索引已初始化,索引已初始化,索引已初始化,索引已初始化,索引已初始化;
布尔索引第二个索引圈已初始化,索引第二个索引圈已初始化;
公众:
//---模拟中的唯一标识符
int-id;
//---速度和速度
Point preferredVelocity;//新计算的速度*在*局部碰撞避免之前
Point newVelocity;//新计算的速度(+碰撞避免),将在“下一步”模拟步骤中应用
float total_max_speed;//整个模拟过程中可能的最大速度
float max_speed;//此时的最大速度
float min_desired_speed;//角色不静止时尝试达到的最小速度
最后一个吸引点;
//---IRM参数
CMMPinterface*CMMPlementation;//走廊内要遵循的指示路线类型,例如“最短路径”或“加权侧”。
//仅用于加权侧:
float sidePreference;//偏向于跟随走廊的某个“边”。必须介于-1(左)和1(右)之间。
float sidePreferenceNoise;//将在每个路由元素处添加到sidePreference的额外噪波系数。
//用于加权_边和最短_路径
浮动首选_间隙;//代理希望远离障碍物的距离(m)。
//---微观仿真模型(例如,用于避免角色之间的碰撞)
MicroInterface*microImplementation;//要使用的本地模型
short micro_maxnrinects;//要在本地模型中检查的邻居数
浮动micro_personalSpaceRadius;//角色物理半径之上的个人空间半径(m)。
//进入此磁盘(radius+个人空间)将被视为“碰撞”。
//---道路/路径指针
node_ptr index_bb;//主干路径上的点(用于计算吸引力)
node_ptr index_bb_cp;//主干路径上的点(用于计算最近的点)
curve_ptr index_ir_circle;//到与圆相交的指示路线上最后一点的索引
浮动索引_ir_circle_mu;//系数wrt指向与圆相交的指示路线上的点
朋友字符;//只有字符类可以查看私有成员(WVT:丑陋C++实践,但它确实有效)
字符设置(int\u id,
//速度
浮动-总最大速度,浮动-最小所需速度,
//指示路线的类型
CMM接口*\u CMM实施、浮动侧首选项、浮动侧首选项噪声、浮动间隙、,
//微观仿真模型的类型
微接口*_微实现):
id(id)、总最大速度(总最大速度)、最小期望速度(最小期望速度),
CMM实施(_CMM实施)、侧偏好(_侧偏好)、侧偏好噪声(_侧偏好噪声)、首选间隙(_间隙),
微实现
{
//速度
新速度=点(0,0);
最大速度=总最大速度;
首选速度=点(0,0);
//道路/IRM指针
索引_bb_初始化=false;
索引_bb_cp_initialized=false;
索引\u ir\u圆\u初始化=假;
索引\u ir\u圆\u mu\u初始化=假;
//默认微设置
micro_maxnr=5;//卡拉莫扎斯2010的默认值:5
micro_personalSpaceRadius=0.0f;//卡拉莫扎斯2010的默认值:0.5
}
};
类字符
{
公众:
点位;
浮动半径;
普雷维波斯点;
int i;//假装是罪魁祸首的东西,没有这个,它工作正常。
//目标数据
点球门;
浮动球门半径;
//状态标志
布尔达到目标;
bool freeze;//角色是否临时冻结
bool;
布尔到达目标集;
点速度;//上次使用的速度
//走廊/路径p
g++ -MM -MG -Ipath/to/includes/that/should/be/part/of/dependency *.cpp