Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:24:40+00:00 2026-05-26T11:24:40+00:00

Trying to initialize a class called StackAsLinkedList, which should be a derived class of

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:24:40+00:00Added an answer on May 26, 2026 at 11:24 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to initialize TinyMCE with JQuery for a specific class of textarea called
I'm trying to create a class called HighscoresController which subclasses NSObject. When I call
how to initialize a private static member of a class in java. trying the
I'm trying to write a simple program in VC++ which will just initialize the
I'm trying to initialize an instance of class consoleDroid with consoleDroid console = new
I have a aspx page that, when loading, will initialize a class called, Bill
I am trying to define a class in the global scope which contains some
I'm just learning classes so I'm trying something basic. I have a class called
I'm trying to implement the following: I have an Importer class which registers so
I am trying to initialize my controls in Silverlight. I am looking for something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.