Pointers to classes

This is a discussion on Pointers to classes within the C++ Programming forums, part of the General Programming Boards category; Ok, so I have a class "Camera" in my program. I want to be able to swap cameras, and thus ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Did you include camera.h in engine.cpp?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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?

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    Camera cam;
    Camera *currentCam;
    currentCam=&cam;
    Could you post the entire function surrounding that code?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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);
    }

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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
    ////////////////////////////////////////////////////////////////////

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Because it's global, that may confuse things. Try this:
    Code:
    Camera cam;
    Camera *currentcam=&cam;
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 05:21 AM
  2. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 04:59 AM
  3. Pointers, Classes, and Errors o my!
    By Scottc1988 in forum C++ Programming
    Replies: 12
    Last Post: 03-13-2003, 09:14 PM
  4. Pointers to classes
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2002, 09:25 PM
  5. question time (classes and pointers)
    By swarm in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21