• 选择一个称为“StackAsLinkedList”的班级,该班应当是抽象的“Stack”(测试代码,可在此查阅:)。
However, I get an error trying to instantiate this code in main():
StackAsLinkedList stack;
error C2259: StackAsLinkedList : cannot instantiate abstract class
我对此感到困惑,因为我认为StackAsLinkedList被定义为圣克的一个衍生类别:
#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
执行:
#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());
}
集装箱级:
#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
但是,Stack公司是否继承了Wrapper——因此,我猜测这意味着圣克还需要采用另一种比较方法? 不知道原始作者是如何去做工作的(头等)。