Reputation: 1853
Trying to initialize a class called StackAsLinkedList, which should be a derived class of the abstract class Stack (testing code which is available here: http://www.brpreiss.com/books/opus4/).
However, I get an error trying to instantiate this code in main():
StackAsLinkedList stack;
error C2259: 'StackAsLinkedList' : cannot instantiate abstract class
I am confused about this, because I thought StackAsLinkedList is defined as a derived class of Stack:
#ifndef STACK_H
#define STACK_H
#include "object.h"
#include "linkList.h"
#include "container.h"
class Stack : public virtual Container
{
public:
virtual Object& Top () const = 0;
virtual void Push (Object&) = 0;
virtual Object& Pop () = 0;
};
class StackAsLinkedList : public Stack
{
LinkedList<Object*> list;
class Iter;
public:
StackAsLinkedList () : list() {}
~StackAsLinkedList() { Purge(); }
//
// Push, Pop and Top
//
void Push(Object& object);
Object& Pop();
Object& Top() const;
//
// purge elements from, and accept elements onto, the list
//
void Purge();
void Accept (Visitor&) const;
friend class Iter;
};
class StackAsLinkedList::Iter : public Iterator
{
StackAsLinkedList const& stack;
ListElement<Object*> const* position;
public:
Iter (StackAsLinkedList const& _stack) : stack(_stack) { Reset(); }
//
// determine whether iterator is pointing at null
//
bool IsDone() const { return position == 0; }
//
// overloaded dereference and increment operator
//
Object& operator*() const;
void operator++() const;
void Reset() { position = stack.list.Head(); }
};
#endif
The implementation:
#include "stack.h"
void StackAsLinkedList::Purge()
{
if ( IsOwner() )
{
ListElement<Object*> const* ptr;
for(ptr = list.Head(); ptr != 0; ptr = ptr->Next() )
delete ptr->Datum();
list.Purge();
count = 0;
}
}
void StackAsLinkedList::Push(Object& object)
{
list.Prepend(&object);
++count;
}
Object& StackAsLinkedList::Pop()
{
if(count == 0)
throw domain_error ("stack is empty");
Object& result = *list.First();
list.Extract(&result);
--count;
return result;
}
Object& StackAsLinkedList::Top() const
{
if(count == 0)
throw domain_error ("stack is empty");
return *list.First();
}
void StackAsLinkedList::Accept(Visitor& visitor) const
{
ListElement<Object*> const* ptr;
for(ptr = list.Head(); ptr != 0 && !visitor.IsDone(); ptr = ptr->Next())
visitor.Visit(*ptr->Datum());
}
class Container:
#ifndef CONTAINER_H
#define CONTAINER_H
#include "object.h"
#include "visitor.h"
#include "iterator.h"
#include "ownership.h"
class Container : public virtual Object, public virtual Ownership
{
protected:
unsigned int count;
Container () : count(0) {}
public:
virtual unsigned int Count () const { return count; }
virtual bool IsEmpty () const { return Count () == 0; }
virtual bool IsFull () const { return false; }
//virtual HashValue Hash () const;
virtual void Put (ostream&) const;
virtual Iterator& NewIterator () const { return *new NullIterator (); }
virtual void Purge () = 0;
virtual void Accept (Visitor&) const = 0;
};
#endif
EDIT: It appears that the compiler says that the CompareTo() method in Object is not implemented in any of the derived classes. However, this functionality is implemented in the derived class of Object called "Wrapper":
#ifndef WRAPPER_H
#define WRAPPER_H
#include "object.h"
template <class T>
class Wrapper : public Object
{
protected:
T datum;
int CompareTo (Object const&) const;
public:
Wrapper ();
Wrapper (T const&);
Wrapper& operator = (T const&);
operator T const& () const;
//HashValue Hash () const;
void Put (ostream&) const;
};
//
// typedefs for for Wrappers representing different primitive
// data types
//
typedef Wrapper <int> Int;
typedef Wrapper <char> Char;
typedef Wrapper <double> Double;
typedef Wrapper <std::string> String;
#include "wrapper.inc"
#endif
But Stack doesn't inherit from Wrapper - so I am guessing this means another CompareTo method needs to be implemented for Stack? Not sure how the original author got this to work (scratches head).
Upvotes: 0
Views: 327
Reputation: 283733
Since you've now explained you're trying to fix it, I suggest:
First step is to get it compiling, which you can do by adding a CompareTo(Object&) const
member to StackAsLinkedList
. You can use either dynamic_cast
or the Visitor
machinery to find out whether the object compared to is another collection.
Next, get rid of reference parameters in any case where the object will be stored by the callee and used after the function returns. And eradicate reference return types, where ownership is being transferred. You can either use pointers, or change the collection to pass-by-value (but don't pass-by-value if the collection should be polymorphic). You'd get:
class Stack : public virtual Container
{
public:
virtual Object& Top () const = 0; // short-term access to object, no ownership transfer, reference is ok here.
virtual void Push (Object*) = 0; // pointer kept, ownership transfer, use pointer
virtual Object* Pop () = 0; // ownership transfer (caller must delete), use pointer
};
Then, you should do something about the brokenness in the Visitor
implementation. Right now, Accept
always calls Visit(Object&)
regardless of the dynamic type. You'd need to call a virtual Accept
function on each individual member, in order to let the Visitor
perform correctly on polymorphic collections.
We're well on the way to scrapping the design by this point.
Upvotes: 3