【问题标题】:Cannot allocate an object of abstract type SortedListHasA无法分配抽象类型 SortedListHasA 的对象
【发布时间】:2014-11-09 00:58:46
【问题描述】:

我在网上做了一些研究,但仍然无法解决我在作业中遇到的问题。

编译器给出的错误是:

test.cpp: In function ‘int main()’:
test.cpp:13:39: error: cannot allocate an object of abstract type ‘SortedListHasA<std::basic_string<char> >’
  testPtr = new SortedListHasA<string>();
                                       ^
In file included from test.cpp:2:0:
SortedListHasA.h:10:7: note:   because the following virtual functions are pure within ‘SortedListHasA<std::basic_string<char> >’:
 class SortedListHasA : public SortedListInterface<ItemType>
       ^
In file included from SortedListHasA.h:5:0,
                 from test.cpp:2:
SortedListInterface.h:35:13: note:  int SortedListInterface<ItemType>::getPosition(const ItemType&) [with ItemType = std::basic_string<char>]
 virtual int getPosition( const ItemType& anEntry) = 0;
             ^

我认为编译器在抱怨如何在派生类中没有重新定义虚函数,但我在 LinkList.cpp 文件中做了,代码所在的位置:

template < class ItemType> 
int LinkedList<ItemType>::getPosition(const ItemType& anEntry) const
{ 
    int position = 0; 
    if (!isEmpty()) 
    { 
        int index = 0; 
        Node<ItemType>* curPtr = headPtr; 
        while ((position == 0) && (index < itemCount)) // While not found 
        { 
            if (anEntry == curPtr->getItem()) 
            position = index + 1; // anEntry is located 
            else 
            { 
                index++; 
                curPtr = curPtr->getNext(); 
            } // end if 
        } // end while 
    } // end if 

 return position; 
}

我的测试代码如下所示:

int main()
{

    SortedListInterface<string>* testPtr = NULL;

    testPtr = new SortedListHasA<string>();
/*
    testPtr -> insertSorted("add");
    testPtr -> insertSorted("subtrac");
*/
    return 0;
}

类 SortedListHasA 是:

/** ADT sorted list using the ADT list.
@file SortedListHasA.h */
#ifndef _SORTED_LIST_HAS_A
#define _SORTED_LIST_HAS_A
#include "SortedListInterface.h"
#include "ListInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template < class ItemType>
class SortedListHasA : public SortedListInterface<ItemType>
{
private :
    ListInterface<ItemType>* listPtr;
public :
    SortedListHasA();
    SortedListHasA(const SortedListHasA<ItemType>& sList);
    virtual ~SortedListHasA();
    void insertSorted( const ItemType& newEntry);
    bool removeSorted( const ItemType& anEntry);
    int getPosition( const ItemType& newEntry) const;
    // The following methods have the same specifications
    // as given in ListInterface:
    bool isEmpty() const ;
    int getLength() const ;
    bool remove(int position);
    void clear();
    ItemType getEntry( int position) const throw(PrecondViolatedExcep);
}; // end SortedListHasA
#include "SortedListHasA.cpp"
#endif

派生自SortedListInterface.h:

/** Interface for the ADT sorted list
@file SortedListInterface.h */
#ifndef _SORTED_LIST_INTERFACE
#define SORTED_LIST_INTERFACE
template < class ItemType>
class SortedListInterface
{
public :

virtual bool removeSorted( const ItemType& anEntry) = 0;

virtual int getPosition( const ItemType& anEntry) = 0;

virtual bool isEmpty() const = 0;

virtual int getLength() const = 0;

virtual bool remove( int position) = 0;
virtual void clear() = 0;

virtual ItemType getEntry( int position) const = 0;
}; // end SortedListInterface
#endif

感谢任何帮助和建议!

【问题讨论】:

  • 编译器的意思是“嘿伙计,class SortedListHasA 是纯抽象的。我不能创建这个类的对象”
  • 但是类 SortedListHasA 不是纯虚类
  • 编译器明确指出virtual int getPosition( const ItemType&amp; anEntry) = 0; = 0 在getPosition 的声明中将class SortedListHasA 声明为纯虚拟。
  • @40two 类 SortedListHasA 派生自 SortedListInterface.h,是一个纯虚类。我已经编辑了我的帖子,很抱歉不清楚。

标签: c++ class virtual abstract


【解决方案1】:

OK 事情已解决,只需更改为:

virtual int getPosition( const ItemType& anEntry) const = 0;

在class SortedListInterface 定义中。您在成员函数 getPosition 的声明中缺少 const 限定符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多