I am making a Camera class in c++ that I use for OpenGL. When I try to print any variable that was declared in Camera.h, the program crashes. But it does not crash if I set or get the value of the variable What am I doing wrong?
Camera.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
Camera.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;
}
The crash message I get is:
opengl.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
Edit
If I put the line float xPos, yPos, zPos; in the public section in Camera.h, and call
Camera mainCamera(0.0f, 0.0f, 0.0f);
cout << "mainCamera x = " << mainCamera.xPos << endl;
…in the main.cpp, it works just fine and prints:
mainCamera x = 0
Well, I figured it out. And this one was a dumb one. I forgot to include
windows.hinMain.cpp, which for some reason weird reason it stopped floats from being printed out (???). It works perfectly now.