Thread: class header confusion

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    class header confusion

    Im having problems understanding headers, my background is in java programming so im not used to having to create header files and i dont really understand their use. Here are two classes ive put together but i dont know if i should use a header or how to put them together using headers or not.

    At the moment the cannonrun class compiles with an error as i attempt to create an instance of the other class. Ive made an attempt to create a header file but obviously its not correct. Do i need a header to link them? is it recommended and what missing?

    The cannonrun class creates a window and positions a cannon ball in the center when a key is pushed it should create an instance of the cannon class and then evolve the instance to move the cannon ball along a path. (should but doesnt at the moment due to failed header).

    cannon.cpp
    Code:
    #include "cannon.h"
    #include <windows.h>
    #include <iostream.h>
    #include <math.h>
    #include <glut.h>
    
    float myrand(){return((1.0*rand())/RAND_MAX);}
    
    class cannon
    {
    	public:
    		float getx();
    		float gety();
    		float getz();
    		float getvx();
    		float getvy();
    		float getvz();
    		cannon(float ,float, float);
    		void  evolve(float t);
    		private:float x,y,z,vx,vy,vz,time;   
    		static float g,c_o_r;
    };
    
    
    float cannon::g=0.1;
    float cannon::c_o_r=0.5;
    float cannon::getx(){ return x; }
    float cannon::gety(){ return y; }
    float cannon::getz(){ return z; }
    float cannon::getvx(){ return vx; }
    float cannon::getvy(){ return vy; }
    float cannon::getvz(){ return vz; }
    
    
    cannon::cannon(float anglex, float anglez,  float mag )
    {
    	x=0,y=0,z=0, time=0;
    	vz = mag*cos((3.14/2)-anglez);
    	vx = cos(anglez)*cos(anglex);
    	vy = mag * cos(anglez)*sin(anglex);
    }
    
    
    void cannon::evolve(float delta)
    {
    	time+=delta;
    	y+=vy*delta;
    	x+=vx*delta;
    	z+=vz*delta;
    	cout << "x= " << x << " y= " << y << " z = " << z << "\n";
    	cout << "vx= " << vx << "vy= " << vy << " vz = " << vz << "\n";      
    	vz+=-g*delta;
    
    	if(z<0)
        {
    		vz=-1*c_o_r; // bounce
    		z=.001; // make sure ball is above ground
        }
    }
    cannonrun.cpp
    Code:
    #include "cannon.h"
    #include <windows.h>
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    #include <glut.h>
    
    
    float angle1=3.0f;
    float angle2=0.0f;
    double mag=0;
    double move=0;
    char length[20];
    double rotateangle=0;
    double rotateangle1=0;
    double rotateangle2=0;
    
    
    void init(void)
    {	
    	glEnable(GL_DEPTH_TEST);
    	glClearColor(0.0f,0.0f,0.0f,0.5f);
    }
    
    void cannonBall(double x,double y,double z)
    {
    	glTranslatef(x,y,z);
    	glColor3f(0.3,0.3,0.3);  
    	glutSolidSphere(0.1,20,20);
    }
    
    
    void display(void)
    {
    
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	
    	glRotatef(rotateangle,0,1,0);
    	glRotatef(rotateangle1,1,0,0);
    	glRotatef(rotateangle2,0,0,1);
    
    	//Draw Ball
    	cannonBall(0.0,0.0,move);
    
    	//glLoadIdentity();
    	//glEnd();
    	glFlush();
    	glutSwapBuffers();
    }
    
    
    void fire()
    {
        while(true)
        {
    		float angle1=0;
    		float angle2=0;
    		float mag=0;
         
    		cannon * cball = new cannon(angle1,angle2,mag);
    	    float time =0;
    	    float delta=0.01;
    
    		int f =3;
    
    	    while(f < 6)
    		{		
    			//gball->evolve(delta);				
    	    }
    	}
    }
    
    void anim()
    {
    	display();
    }
    
    void keyboard( unsigned char key, int x, int y)
    {
      char c='#';
      
      switch(key)
        {
    	case('s'):
    	  angle1+=5; if(angle1>180)angle1=180;
    	  break;
    	case('a'):
    	  angle1-=5; if(angle1<0)angle1=0;
    	  break;
    	case('w'):	  
    	  angle2+=5; if(angle2>90)angle2=90;
    	  break;
    	case('x'):
    	  angle2-=5; if(angle2<0)angle2=0;
    	  break; 
    	case('o'):
    	  mag+=0.1;
    	  break;
    	case('p'):
    	  mag+=-0.1; if(mag<0)mag=0;
    	  break;
    	case('m'):
    	  move=move+0.1;
    	  break;
    	case('n'):
    	  move=move-0.1;
    	  break;
    	case('u'):
    	  rotateangle=rotateangle-5;
    	  break;
    	case('t'):
    	  rotateangle1=rotateangle1-5;
    	  break;
    	case('y'):
    	  rotateangle2=rotateangle2-5;
    	  break;
    
        }
    
      glutPostRedisplay();
    
    }
    	
    
    void reshape(int w, int h)
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(0, 1.0, 0.1, 40);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    
    int main( int argc, char * argv[])
    {
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
       glutInitWindowSize (600,600);  
       glutCreateWindow(argv[0]);
       init();
       glutReshapeFunc(reshape);
       glutKeyboardFunc(keyboard); 
       glutDisplayFunc(display);
       //glutIdleFunc(anim);
       glutMainLoop();
       return 0;
    }
    cannon.h
    Code:
    #include <windows.h>
    #include <iostream.h>
    #include <math.h>
    #include <stdlib.h>
    #include <glut.h> 
    
    #ifndef CANNON_H
    #define CANNON_H
     
    
    	cannon(float anglex, float anglez,  float mag );
    
     #endif
    Apologies for the massive amount of code posted but im really stuck on this header problem and ive tried looking in books for solutions but i need a practical one in order to help me. Many thanks.

    --- Please note i have used glut libraries just comment out the lines of code with glut reference if u do not have it installed
    Last edited by te5la; 07-22-2008 at 02:10 PM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Put the class definition:
    Code:
    class cannon {...};
    in the header, and the implementation of the member functions in the .cpp file. Avoid including headers in your header, and in this case it doesn't look like you need any since all your members are primitive types.
    Last edited by medievalelks; 07-22-2008 at 02:21 PM.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    got it working now thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM