Попытка инициализировать класс StackAsLinkedList, который должен быть производным классом абстрактного класса Stack (тестовый код, который доступен здесь: http://www.brpreiss.com/books/opus4/ ).
Тем не менее, я получаю сообщение об ошибке, пытающееся создать экземпляр этого кода в main ():
StackAsLinkedList stack;
error C2259: 'StackAsLinkedList' : cannot instantiate abstract class
Я смущен этим, потому что я думал, что StackAsLinkedList определяется как производный класс 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
Реализация:
#include "stack.h"
void StackAsLinkedList::Purge()
{
if ( IsOwner() )
{
ListElement 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 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: Похоже, компилятор говорит, что метод CompareTo () в Object не реализован ни в одном из производных классов. Однако эта функциональность реализована в производном классе объекта под названием «Wrapper»:
#ifndef WRAPPER_H
#define WRAPPER_H
#include "object.h"
template
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;
typedef Wrapper Char;
typedef Wrapper Double;
typedef Wrapper String;
#include "wrapper.inc"
#endif
Но Stack не наследует от Wrapper - поэтому я предполагаю, что это означает, что для Stack должен быть реализован другой метод CompareTo? Не знаете, как оригинальный автор получил это на работу (царапины головы).
Поскольку вы теперь объяснили, что пытаетесь это исправить, я предлагаю:
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.
К этому моменту у нас все в порядке.