我有一个C ++对象,我正在我的视图控制器中获取引用,如下所示:
m_applicationEngine = (ApplicationEngine *)[(GLView *)[self.parentViewController view] m_applicationEngine];
在我的位置委托中,我在类中调用了methid,如下所示:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coordinate = [newLocation coordinate];
NSLog(@"Location acquired %f, %f", coordinate.latitude, coordinate.longitude);
myLocation = new Location(coordinate.latitude, coordinate.longitude);
m_applicationEngine->GotoLoc(myLocation);
}
它确实调用了GotoLoc函数,但是当函数第一次访问m_applicationEngine实例的公共成员变量时它崩溃了。我很肯定会员已经初始化了。有人有什么想法吗?
m_applicationEngine是在GLView:UIView中的initWithFrame()中初始化的C ++对象。
m_applicationEngine = new ApplicationEngine(m_renderingEngine, self);
这是GotoLoc()的代码。它第一次引用m_rotation,它是类崩溃的成员变量。我可以在代码的其他区域调用GotoLoc(),而不是在上面的Obj-C委托函数中。
void ApplicationEngine::GotoLoc(Location *location) {
vec3 eye = vec3(0, 0, 1);
vec3 end = location->loc;
// Compute the difference between where we're looking at now vs. where we want to point
mat4 transpose;
MatrixTranspose(transpose, m_rotation);
MatrixVec3Multiply(eye, eye, transpose);
//cout << "Inverse. I was here x:" << relativeVec.x << " y:" << relativeVec.y << " z:" << relativeVec.z << endl;
// bring vectors down to the equator to measure x angle
vec3 relativeX = vec3(eye.x, 0, eye.z);
// reference vector is the rotation axis for the sign to be correct
float xAngle = signed_angle(relativeX, vec3(end.x, 0, end.z), vec3(0,-1,0));
mat4 rotationX;
MatrixRotationY(rotationX, -1 * xAngle);
MatrixMultiply(m_rotation, rotationX, m_rotation);
// need to rotate eye vector to line up on y with new target to measure y angle
MatrixTranspose(transpose, rotationX);
MatrixVec3Multiply(eye, eye, transpose);
float yAngle = signed_angle(eye, end, vec3(1,0,0));
cout << "Went to difference of: ANGLE x: " << RADIANS_TO_DEGREES(xAngle) << " y:" << RADIANS_TO_DEGREES(yAngle) << endl;
mat4 rotationY;
MatrixRotationX(rotationY, -1 * yAngle);
MatrixMultiply(m_rotation, m_rotation, rotationY);
}
答案 0 :(得分:0)
最好猜测你给我们的信息是m_applicationEngine实际上不是C ++对象。你在哪里创造它?在init?在存取器m_applcationEngine中?
如果您想要更好的答案,请向我们展示GotoLoc()的代码以及如何初始化C ++对象。
修改(关注新发布的代码):
我认为m_applicationEngine
为空。您应该能够通过在GotoLoc()
的第一行放置一个中断并检查this
(是否适用于C ++?我希望等效于self
)指针来确认。
如果您的视图位于nib文件中,我会感觉initWithCoder:
代替initWithFrame:
,这可以解释为什么永远不会构造您的C ++对象。解决此问题的最简单方法是通过属性创建和访问它,并在首次使用时初始化它,例如
// .h file
@property (readonly, assign) ApplicationEngine* applicationEngine;
// .m file
-(ApplicationEngine*) applicationEngine
{
if (m_applicationEngine == NULL)
{
m_applicationEngine = new ApplicationEngine(m_renderingEngine, self);
}
return m_applicationEngine;
}