English 中文(简体)
H/W:C++ 傲慢“初步确定摘要基类”
原标题:H/W: C++ Error "trying to initialize Abstract base class"

• 选择一个称为“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——因此,我猜测这意味着圣克还需要采用另一种比较方法? 不知道原始作者是如何去做工作的(头等)。

最佳回答

既然你现在解释你试图纠正这一说法,我建议:

  • 第一步是将其汇编成册,通过添加“<条码>即可做到这一点。 比较(Object&) 您可使用<代码>动力学-cast或Visitor机制,以发现该物体是否与另一卷相比。

  • 其次,在目标由被点名人储存并在功能恢复后使用的任何情况下,都排除了参考参数。 并且消除了转让所有权的参考回报类型。 你们要么使用点,要么把收集工作改变为逐值(但如果收集工作应是多变式的,则不会按逐值计算)。 您:

    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
    };
    
  • 然后,您应就<代码>Visitor执行中的破碎之处做一些事情。 现在,Accept 无论动态类型如何,都总是打上<条码>。 您需要为每个成员指定一个虚拟的<代码>Accept功能,以便<代码>Visitor在多形态收集上正确进行。

我们回过了在这一点之前取消设计的道路。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签