Jump to content
  • Advertisement
Sign in to follow this  
startreky498

Using abstract class in another abstract class

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've created two abstract classes which I intend to inherit in further classes. Here's what I've got:
//sprite.h
#ifndef SPRITE_H
#define SPRITE_H

class SPRITE
{
public:
	virtual void Release()=0;
};

#endif

//graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "Sprite.h"

class Graphics 
{
public:
	virtual SPRITE registerImage(char *path, transparency color)=0;
{

#endif

And I've created two more classes, "d3dSprite" and "dx9", to inherit SPRITE and Graphics respectively. For example: bool drawImage(d3dSprite *sourceImage, const RECT *rSource, const POINT *pDest); Problem is, when I try to compile, I get the following error:
c:\Reality 101\C++ Game Engine\Text\01 Basic Text\Graphics.h(31) : 
    error C2259: 'SPRITE' : cannot instantiate abstract class
        due to following members:
        'void SPRITE::Release(void)' : is abstract
        c:\Reality 101\C++ Game Engine\Text\01 Basic Text\Sprite.h(7) : 
            see declaration of 'SPRITE::Release'
C++ is obviously being very picky about my classes. I'm going to inherit these classes in other classes, so this problem shouldn't be an issue in those classes. I just need to get past this error. What can I do to fix this? Any help is much appreciated. Thanks.
Edit by Fruny: formatting.

Share this post


Link to post
Share on other sites
Advertisement
All inherited pure virtual functions must be overriden in the derived class or the derived class will also be considered abstract.

If you plan on using the abstract classes polymorphically (i.e. as interfaces), you should also make their destructor virtual, lest Bad Things™ happen to you.

Share this post


Link to post
Share on other sites
The problem is that SPRITE is returned by value from registerImage.

Suppose that was valid: You would create a subclass of SPRITE inside the function and the return value would have to be converted to the abstract base SPRITE. This could be made by removing all added functionality of the subclass (slicing), and you would be left with an unusable abstract object.

To solve the problem change the declaration to either a reference or a pointer:
virtual SPRITE* registerImage(char *path, transparency color)=0;

Share this post


Link to post
Share on other sites
Abstract class is a class that can not have any instance (not including instances of classes that inherit from the base class and define all pure virtual functions declared in the base class)

By declaring Graphics member function as:
virtual SPRITE registerImage(char *path, transparency color)=0;
you try to make an object of an abstract class SPRITE (that is illegal).

//

Try to return SPRITE as reference or pointer.

virtual const SPRITE& registerImage(char *path, transparency color)=0;
or
virtual const SPRITE* registerImage(char *path, transparency color)=0;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!