-
Pointers to classes
Ok, so I have a class "Camera" in my program. I want to be able to swap cameras, and thus have the move function act on a pointer to the current camera. So I did this:
Code:
Camera cam;
Camera *currentCam;
currentCam=&cam;
but I recieve the error message:
Code:
--------------------Configuration: CurrentErikEngine - Win32 Debug--------------------
Compiling...
EngineMain.cpp
C:\Program Files\Microsoft Visual Studio\openGL\Strnad\Project\Recent\CurrentErikEngine\EngineMain.cpp(84) : error C2501: 'currentCam' : missing storage-class or type specifiers
C:\Program Files\Microsoft Visual Studio\openGL\Strnad\Project\Recent\CurrentErikEngine\EngineMain.cpp(84) : error C2040: 'currentCam' : 'int' differs in levels of indirection from 'class Camera *'
C:\Program Files\Microsoft Visual Studio\openGL\Strnad\Project\Recent\CurrentErikEngine\EngineMain.cpp(84) : error C2440: 'initializing' : cannot convert from 'class Camera *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
CurrentErikEngine.exe - 3 error(s), 0 warning(s)
Why is that?
Thanks!
sirSolarius
-
Can you post some compilable code that exhibits the problem? It has nothing to do with the three lines you posted, they compile fine for me when I create a dummy Camera class.
-
Sure, thanks!
Code:
// amera.h: interface for the Camera class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_AMERA_H__1B5F6468_12D8_421A_97B8_DF7EA2D6E923__INCLUDED_)
#define AFX_AMERA_H__1B5F6468_12D8_421A_97B8_DF7EA2D6E923__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
#include <gl/gl.h>
#include <math.h>
#include "Object1.h"
class Camera : public Object
{
public:
Camera();
~Camera();
void moveForward(GLfloat amount);
void moveLeft(GLfloat amount);
void lookUp(GLfloat amount);
void setUpDownLimits(GLfloat newLower, GLfloat newUpper);
void bounce();
void draw();
protected:
GLfloat walkBias;
GLfloat bounceAmount;
GLfloat upperLimit;
GLfloat lowerLimit;
};
#endif // !defined(AFX_AMERA_H__1B5F6468_12D8_421A_97B8_DF7EA2D6E923__INCLUDED_)
If there's anything else that you would need to see, just ask!
Thanks again,
sirSolarius
-
Did you include camera.h in engine.cpp?
-
Yup.
Code:
/////////////////////////////////////////////////////////////////////
////// CLASSES /////////////////////////
/////////////////////////////////////////////////////////////////////
#include "Object1.h" // The Base Object Class
#include "amera.h" // The Camera Class
#include "Box.h" // The Box Class
////////////////////////////////////////////////////////////////////
I'm reading online and I found something about "copy constructors." Would those apply here?
-
Code:
Camera cam;
Camera *currentCam;
currentCam=&cam;
Could you post the entire function surrounding that code?
-
I'm confused... which function?
All I did was write a Camera class with some basic methods, and then I tried to make the pointer. I don't have anything in my code about it, which I assume is bad.
Code:
// amera.cpp: implementation of the Camera class.
//
//////////////////////////////////////////////////////////////////////
#include "amera.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Camera::Camera()
:Object(),upperLimit(30.0f),lowerLimit(-60.0f),bounceAmount(0.12f),walkBias(0.0f)
{
}
Camera::~Camera()
{
}
//////////////
//Rotation
//////////////
void Camera::lookUp(GLfloat amount)
{
rotX+=amount;
if(rotX > upperLimit)
rotX=upperLimit;
else if(rotX < lowerLimit)
rotX=lowerLimit;
}
void Camera::setUpDownLimits(GLfloat newLower, GLfloat newUpper)
{
upperLimit = newUpper;
lowerLimit = newLower;
}
//////////////
//Movement
//////////////
//PI divided 180 = 0.01745329252f
void Camera::moveForward(GLfloat amount)
{
x-=(GLfloat)(sin(rotY*0.01745329252f)* amount);
z+=(GLfloat)(cos(rotY*0.01745329252f)* amount);
bounce();
}
void Camera::moveLeft(GLfloat amount)
{
x-=(GLfloat)(sin((rotY+90)*0.01745329252f)* amount);
z+=(GLfloat)(cos((rotY+90)*0.01745329252f)* amount);
bounce();
}
void Camera::bounce()
{
walkBias+=bounceAmount;
if(walkBias >= 360.0f) //Calculate bounce
walkBias -=360.0f;
y+=(float)(sin(walkBias*0.01745329252f))/1.5f; //Make player bounce
}
////////////
//Draw
///////////
void Camera::draw()
{
glRotatef(rotX, 1.0f, 0.0f, 0.0f);
glRotatef(rotY, 0.0f, 1.0f, 0.0f);
glRotatef(rotZ, 0.0f, 0.0f, 1.0f);
glTranslatef(x, y, z);
}
-
No, I mean, post all of the code around the code that caused the error.
Code:
Camera cam;
Camera *currentCam;
currentCam=&cam;
This is the code that caused the error right? Post the function that contains that code.
-
Oooooooooh. Sorry!
Code:
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include <stdio.h> // Header File For Standard Input/Output
#include <stdarg.h> // Header File For Variable Argument Routines
#include <math.h> // Math Functions
/////////////////////////////////////////////////////////////////////
////// CLASSES /////////////////////////
/////////////////////////////////////////////////////////////////////
#include "Object1.h" // The Base Object Class
#include "amera.h" // The Camera Class
#include "Box.h" // The Box Class
////////////////////////////////////////////////////////////////////
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
//////////////////////////////////////////////////////////////////
////// *IMPORTANT CONSTANTS* /////////////////////
//////////////////////////////////////////////////////////////////
const double PI=3.1415926545;
const double PI_OVER_180=PI/180.0;
const GLfloat MOVE_VALUE = 1.0f;
//////////////////////////////////////////////////////////////////
double framesSinceJump=0;
GLfloat travelled;
bool jumping=false;
GLfloat velocity=0.0f;
bool crouch=false;
//////////////////////////////////////////////////////////////////
////// *INPUT* /////////////////////
//////////////////////////////////////////////////////////////////
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
bool light; // Lighting ON/OFF
bool lp; // L Pressed?
bool fp; // F Pressed?
bool lc;
POINT mousePoints; // Coords of the Mouse
bool realMouse=true;
/////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////// *TEXT* ////////////////////
///////////////////////////////////////////////////////////////////
GLuint base; // Base Display List For The Font Set
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////// *THE CAMERA* //////////////////////
///////////////////////////////////////////////////////////////////
Camera cam;
Camera *currentCam;
currentCam=&cam;
GLfloat z=-5.0f; // Depth Into The Screen
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////// *PHYSICS* /////////////////////
//////////////////////////////////////////////////////////////////
const GLfloat METERS_TO_FLOAT = 2.0f;
GLfloat gravity = 4.9f;
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////// *LIGHTING* //////////////////////
///////////////////////////////////////////////////////////////////
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
////// *TEXTURES* //////////////////////
///////////////////////////////////////////////////////////////////
GLuint filter; // Which Filter To Use
GLuint texture[3]; // Storage For 3 Textures
////////////////////////////////////////////////////////////////////
-
Because it's global, that may confuse things. Try this:
Code:
Camera cam;
Camera *currentcam=&cam;