打印标头中声明的变量时C++崩溃
C++ crashes when printing variable declared in header
我正在用 c++ 制作一个用于 OpenGL 的Camera
类。当我尝试打印在 Camera.h
中声明的任何变量时,程序崩溃了。但是如果我设置或获取变量的值,它不会崩溃我做错了什么?
相机.h
#ifndef CAMERA_H
#define CAMERA_H
class Camera
{
public:
Camera();
Camera(float xP, float yP, float zP);
void move(float x, float y, float z);
protected:
private:
float xPos, yPos, zPos;
};
#endif // CAMERA_H
相机.cpp
#include "Camera.h"
#include <iostream>
#include <GL/glut.h>
using namespace std;
Camera::Camera()
{
}
Camera::Camera(float xP, float yP, float zP)
: xPos(xP), yPos(yP), zPos(zP)
{
}
void Camera::move(float x, float y, float z)
{
glTranslatef(-x, -y, -z);
//None of this crashes:
xPos = 1;
yPos = xPos;
//Crashes here:
cout << "mainCamera x = " << xPos << endl;
}
我收到的崩溃消息是:
OpenGL.exe遇到了问题,需要关闭。 对于给您带来的不便,我们深表歉意。
编辑
如果我把线路float xPos, yPos, zPos;
放在Camera.h
的公共部分,然后打电话
Camera mainCamera(0.0f, 0.0f, 0.0f);
cout << "mainCamera x = " << mainCamera.xPos << endl;
。在main.cpp
中,它工作得很好并打印:
主摄像头 x = 0
嗯,我想通了。而这个是愚蠢的。我忘了在Main.cpp
中包含windows.h
,出于某种奇怪的原因,它阻止了浮标被打印出来(???)。它现在运行良好。
#include <windows.h>
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 变量没有改变?通过向量的函数调用