templates and abstract classes

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
rafyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jpk041897Commented:
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
jhanceCommented:
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
nietodCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rafyAuthor Commented:
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
nietodCommented:
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
nietodCommented:
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
nietodCommented:
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
rafyAuthor Commented:
my mistake was with the template declaration...thanks

but how do I give the points to nietod (not jhance??)
0
rafyAuthor Commented:
Please let nietod answer this question so that I can give him his points...thanks!
0
nietodCommented:
You're welcome.  the problem was that you were not storig pointers then?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rafyAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.