Thread: Derived class can't declare

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,071
    EDIT: post removed because I realized that what I suggested would not work/make a difference.
    Last edited by psychopath; 03-20-2006 at 07:49 PM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    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
    968
    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
    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.

  9. #9
    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

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

  11. #11
    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.

  12. #12
    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.

  13. #13
    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-26-2006, 12:59 AM
  2. derived class constructor syntax
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2003, 08: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-29-2002, 12:29 AM
  5. derived class member function pointer
    By btq in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 12:41 PM