Solved

templates and abstract classes

Posted on 1998-03-14
11
234 Views
Last Modified: 2010-04-10
Help!

I am trying to input into a list, objects from an abstract class top_class...

when I call the list constructor
 sortablelist<top_class> L;
I get an error
"cannot create instance of abstract class"  I understand that this is due to the fact that the compiler cannot resolve for the methods (which are only defined in the sub-classes) of this abstract class...

My problem is that I don't really know how to fix this..

Do I need to send a pointer to the object in order to allow polymorphism?

I don't know how since
 sortablelist<top_class*> L;
produces an error... can't find a match for...(above line constructor)
How can I fix this (please don't just say implement this in the sortable list!...I need to know how!), any code would be very helpful.... also if you need the implementation of sortablelist just let me know.

here's what my class looks like

class top_class{
  protected:
         int depth;
         int order;
       
  public:
      top_class(int);            //constructor
        virtual void M1(C&)=0;      //abstract method
        int getdepth();
        int getorder();
};


I need to place these items in the list and also be able to use the getdepth() method from an iterator (this I have)


thanks in advance
0
Comment
Question by:rafy
11 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1183472
If I'm not mistaken, you problem arrises from the fact that an abstract class does not actualy exist in memory and cannot be instantiated.

An abstract class is only a definition of an an object, not a declaration, and as such will not allow either a pointer or a referance to it.

In order to fix this, you will need to derive an object from your abstract class and define all the virtual methods. The definition of these methods needs not do anything usefull, say:

void derived_top_Class:M1(int /*or whatebver*/ C&){
   return;
}

In this fashion, you insure that you are using a declarable object that can be instantiated.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1183473
An instance of an abstract class can never be created.  You must derive a class of your own from the abstract class and you MUST overide each and every abstract method.  So your top_class might be used as follows:

class my_class : top_class
{
   virtual void M1(C&);  // MUST HAVE AN IMPLEMENTATION!
};


Now you can add an instance of your my_class in your program.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1183474
jhance is correct, but I feel a little bit unclear.  And abstract class is a class that has one ore more functios that are "pure virtual"  that is, that are defined using the "= 0" syntax as below.

class abstract
{
 virtual void SomeFunction() = 0;
};

A concretec class has no pure virtual functions.  When you want to derive a concrete class from an abstract class, you must redefine each of the pure abstract functions.  Like

class Concrete : public Abstract
{
 virtual void SomeFunction() { cout << "I'm concrete"; };
};

Note also that it is possible to derive an abstract class from an abstract class (Either by not defining some of the pure virtual functions, or be difining new ones)  Also it is possible to derive an abstract class from a concrete class, by adding pure virtual functions.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:rafy
ID: 1183475
Maybe I wasn't clear...

I declared the method virtual because I DO have it implemented in sub-classes of top class.  The problem is that I have two different subclasses (close to similar but not exact copies), and the sortable list is supposed to store instances from both these subclasses and sort them by the "order" variable which is present in both!

ie this exist

class top_class{
  protected:
         int depth;
         int order;
       
  public:
top_class(int); //constructor
        virtual void M1(C&)=0; //abstract method
        int getdepth();
        int getorder();
};

class der1: public top_class{
     protected:
        some instance vars of this subclass;

      public:
         void M1(C&);   //this IS implemented
};
class der2: public top_class{
     protected:
        instance vars of this subclass;

      public:
         void M1(C&);    //and this IS implemented
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183476
The problem must be the "sortable list", which you haven't show us.  It must be trying to store the base type (top_class) which it can't because it is abstract.  However it can store pointers to the base class,  (top_class *)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183477
if the list is a template type, like list<>, it should be declated as

list<top_class *> SortList;

not as

list<top_class> SortList;

If that doesn't help, post the code for the list.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183478
Looking back, maybe more of an explantation is in order.  The list (almost certainly) cannot store objects of the two derived classes directly.  Few containers can do this because the objects may have different sizes and they do have different types which means that C++'s type checking would have to be circumvented.  Now there are some exceptions to this, but usually if you need to store more than one type of object in a container (list, array, hash table, etc) what you do is to create the objects dynamically using new, and then store pointers to the base class in the container.  This works well since pointers to the derived classes will automatically be converted to pointers to the base classes.  For example.

SortList.Add(new der1);  // add pointer to a der1;
SortList.Add(new der2);  // add pointer to a der2;

Now you have to make sure that the objects in the list are eventually destroyed with delete.  
0
 

Author Comment

by:rafy
ID: 1183479
my mistake was with the template declaration...thanks

but how do I give the points to nietod (not jhance??)
0
 

Author Comment

by:rafy
ID: 1183480
Please let nietod answer this question so that I can give him his points...thanks!
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1183481
You're welcome.  the problem was that you were not storig pointers then?
0
 

Author Comment

by:rafy
ID: 1183482
well, sort of...

I had just forgotten to change the template to the pointer notation (I was only writing the constructor with the *...

stipid eh?

Anyways thanks...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question