-1
我已经在线做了一些研究,但仍无法解决在我的作业分配中遇到的问题。无法分配抽象类型的对象SortedListHasA
由编译器给出的错误是:
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
任何帮助和建议表示赞赏!
编译器说的是“嘿,伙计,'类SortedListHasA'是纯抽象的,我不能创建这个类的对象” – 101010 2014-11-09 01:01:34
,但类SortedListHasA不是纯虚拟类 – 2014-11-09 01:04:16
编译器明确指出'虚拟int getPosition(const ItemType&anEntry)= 0;'getPosition'声明中的'''= 0'声明class SortedListHasA'为纯虚函数。 – 101010 2014-11-09 01:05:40