Derived class can't declare

This is a discussion on Derived class can't declare within the Game Programming forums, part of the General Programming Boards category; Ok, somehow i think this is weird. I'm gonna summarized my whole code: Code: class Sphere; class Rectangle; class Segment; ...

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Derived class can't declare

    Ok, somehow i think this is weird. I'm gonna summarized my whole code:
    Code:
    class Sphere;
    class Rectangle;
    class Segment;
    
    
    class Body
    {
       public: 
       vect2 Velocity, Position;
       float mass
         Body(){}
         //then other stuff and functions will go on
       //I also have some virtual bool function in here named "Collision"
    };
    
    class Sphere: public Body
    {
    public:
       float Radius;
       Sphere(){}
       //Then the collision functions will go on
    
    };
    So that's basically it. Then when i tried to do this:
    Code:
    Sphere ball;
    it gave an error like this:
    Code:
    error C2259: 'Sphere' : cannot instantiate abstract class due to following members:
    see declaration of 'Sphere'
    Can someone help me out here?
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    EDIT: post removed because I realized that what I suggested would not work/make a difference.
    Last edited by psychopath; 03-20-2006 at 06:49 PM.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Try this

    Sphere ball = new body;

    Among other things... Make the base destructor virtual... if you have one.. Nothing else I can think of with the code given..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Are you overriding all your pure virtual functions, like the collision ones?
    Illusion and reality become impartiality and confidence.

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by ichijoji
    Are you overriding all your pure virtual functions, like the collision ones?
    No, not that i know now. I was thinking about it but stuck with the derived class

    @Shamio
    It still give me the same error with your method.
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Got it, i have to use pointer.
    Hello, testing testing. Everthing is running perfectly...for now

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Yeah, I ment to put this:

    Sphere * ball = new body;
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    @Shamio:
    It's still giving me same error but with the Body class.

    So, pointer is not working, can anyone have any suggestions?
    Hello, testing testing. Everthing is running perfectly...for now

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Paste all the code...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    As ichijoji suggested, you probably have not provided a definition in your derived class for a pure virtual function in your base class.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by Shamino
    Yeah, I ment to put this:

    Sphere * ball = new body;
    Don't you mean Body* ball = new Sphere(); ?
    A body is not (neccessarily) a sphere!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    All the virtual function in the Body class had been overrided. Here is my code:
    Code:
    #ifndef objects_h
    #define objects_h
    #include "vector2d.h"
    
    class Event;
    class Sphere;
    class Rect;
    class Segment;
    
    
    
    class Body
    {
    public:
    	vect2 Position;
    	vect2 Velocity;
    	float Mass, InvMass;
    
    	Body(){}
    
    	void Initialize(const vect2& Pos, const vect2& Vel, float o_Mass) //take object's position, Velocity, and mass
    	{
    		Position = Pos;
    		Velocity = Vel;
    		Mass = o_Mass;		
    		InvMass = (Mass > 0.0f)? 1.0f/Mass : 0.0f;
    	}
    
    	virtual void Update(float dt)
    	{
    		Position += Velocity * dt;
    	}
    
    	virtual void ComputeVelocity(float mouse_X, float mouse_Y, float dt)
    	{
    		Velocity = (vect2(mouse_X, mouse_Y) - Position) / dt;
    	}
                    virtual bool Collision(const Body& bBody, Event& eEvent, float tMax)const = 0;
    	virtual bool Collision(const Sphere& bBall,	  Event& eEvent, float tMax) const = 0;
    	virtual bool Collision(const Rect& rRect,	  Event& eEvent, float tMax) const = 0;
    };
    
    class Event
    {
    public:
    
    	vect2 Pcoll, Ncoll, Vcoll;
    	Body* pBody[2];
    
    	union
    	{
    		float tcoll, dcoll;
    	};
    
    	void RunCollision()
    	{
    		vect2 Vel = pBody[0]->Velocity - pBody[1]->Velocity;
    		float mass1 = pBody[0]->InvMass;
    		float mass2 = pBody[1]->InvMass;
    		float totalMass = mass1 + mass2;
    
    		float vn = Vel.Dot(Ncoll);
    		float elasticity = 1.0f;
    		float grip = 0.0f;
    
    		vect2 Vn = vn * Ncoll;
    		vect2 Vt = Vel - Vn;
    
    		Vel = -(1.0f + elasticity) * Vn - Vt * (grip);
    
    		if(mass1 > 0.0f) pBody[0]->Velocity += Vel*(mass1/totalMass);
    
    		if(mass2 > 0.0f) pBody[1]->Velocity -= Vel*(mass2/totalMass);
    
    		//make the object seperate
    
    		vect2 rPosition = pBody[0]->Position - pBody[1]->Position;
    		float sgn = sign(rPosition.Dot(Ncoll));
    
    		float threshold = 0.1f;
    		if(mass1 > 0.0f) pBody[0]->Position += sgn * threshold * Ncoll;
    
    		if(mass2 > 0.0f) pBody[1]->Position -= sgn * threshold * Ncoll;
    
    	}
    
    };
    
    class Segment
    {
    public:
    	vect2 Vertex[2];
    	vect2 Dir;
    	vect2 Perp;
    	float length;
    	Body* bBody;
    
    	Segment(){ bBody = NULL; }
    
    	Segment(const vect2& P0, const vect2& P1, Body *b_Body)
    	{
    		Vertex[0] = P0;
    		Vertex[1] = P1;
    		bBody = b_Body;
    		Dir = Vertex[0] - Vertex[1];
    		length = Dir.Normalize();
    		Perp = Dir.Perp();
    	}
    
    };
    
    class Sphere : public Body
    {
    public:
    	float Radius;
    	int numVer;
    	vect2* Vertices;
    
    	Sphere(){}
    
    	void Initialize(const vect2& Pos, const vect2& Vel, float radius)
    	{
    		Body::Initialize(Pos, Vel, 1.0f);
    		Radius = radius;
    		numVer = 32;
    		Vertices = new vect2[numVer];
    	}
    
    	bool Collision(const Segment& sSegment, Event& eEvent, float& tMax) const;
    
    	bool Collision(const Segment& sSegment, int i, Event& eEvent, float& tMax) const;
    
    	bool Collision(const Body& bBody, Event& eEvent, float& tMax) const
    	{
    		return bBody.Collision(*this, eEvent, tMax);
    	}
    
    	bool Collision(Sphere& sSphere, Event& eEvent, float& tMax) const{ return false; }
    
    	bool Collision(const Rect& rRect, Event& eEvent, float& tMax) const;
    
    	void Render() const
    	{
    	
    		glBegin(GL_TRIANGLE_FAN);
    			for(int j=0; j<numVer; j++)
    			{
    				float a = 2*PI*(j/numVer);
    				Vertices[j] = vect2(cos(a)*Radius + Position.x, sin(a)*Radius + Position.y);
    				glVertex2f(Vertices[j].x, Vertices[j].y);
    			}
    
    		glEnd();
    	}
    
    };
    
    class Rect : public Body
    {
    public:
    	Segment Seg[4];
    	vect2 Extent;
    	Rect(){}
    
    	void Initialize(const vect2& Pos, const vect2& Ext, float mass)
    	{
    		Body::Initialize(Pos, vect2(0, 0), 10.0f);
    		Extent = Ext;
    		UpdateSegment();
    	}
    
    	void UpdateSegment()
    	{
    		vect2 Min = Position - Extent;
    		vect2 Max = Position + Extent;
    
    		Seg[0] = Segment(vect2(Min.x, Min.y), vect2(Max.x, Min.y), this);
    		Seg[1] = Segment(vect2(Max.x, Min.y), vect2(Max.x, Max.y), this);
    		Seg[2] = Segment(vect2(Max.x, Max.y), vect2(Min.x, Max.y), this);
    		Seg[3] = Segment(vect2(Min.x, Max.y), vect2(Min.x, Min.y), this);
    	}
    
    	void Update(float dt)
    	{
    		Body::Update(dt);
    		UpdateSegment();
    	}
    
    	void Render() const
    	{
    		vect2 V[4] = {Seg[0].Vertex[0], Seg[1].Vertex[0], Seg[2].Vertex[0], Seg[3].Vertex[0] };
    		glBegin(GL_QUADS);
    			glVertex2fv(&V[0].x);
    			glVertex2fv(&V[1].x);
    			glVertex2fv(&V[2].x);
    			glVertex2fv(&V[3].x);
    		glEnd();
    	}
    
    	bool Collision(const Body& bBody, Event& eEvent, float tMax)const
    	{
    		return bBody.Collision(*this, eEvent, tMax);
    	}
    
    	bool Collision(const Rect& rRect, Event& eEvent, float tMax)const{ return false; }
    
    	bool Collision(const Sphere& sSphere, Event& eEvent, float tMax) const
    	{
    		bool Collide = false;
    		Collide |= sSphere.Collision(Seg[0], eEvent, tMax);
    		Collide |= sSphere.Collision(Seg[1], eEvent, tMax);
    		Collide |= sSphere.Collision(Seg[2], eEvent, tMax);
    		Collide |= sSphere.Collision(Seg[3], eEvent, tMax);
    
    		return Collide;
    	}
    
    };
    Logically, there would be no problem when declare a Sphere object, but somehow this is getting so weird
    Last edited by hdragon; 03-21-2006 at 09:30 AM.
    Hello, testing testing. Everthing is running perfectly...for now

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    bool Collision(const Rect& rRect, Event& eEvent, float& tMax) const;
    Sphere doesn't implement this pure virtual method!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Nevermind, i forgot to put the '&' sign in the virtual function for "float& tMax" in the Body class. Everything is ok now.
    Hello, testing testing. Everthing is running perfectly...for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-25-2006, 11:59 PM
  2. derived class constructor syntax
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2003, 07:00 PM
  3. Instantiating a derived class from a Base class
    By kwoky in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2003, 01:33 AM
  4. Replies: 4
    Last Post: 12-28-2002, 11:29 PM
  5. derived class member function pointer
    By btq in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 12:41 PM

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