我在网上做了一些研究,仍然无法解决我的家庭作业中遇到的问题。
编译器给出的错误是:
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;
}
class 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
感谢任何帮助和建议!
答案 0 :(得分:2)
确定事情已经解决,只需改为:
virtual int getPosition( const ItemType& anEntry) const = 0;
在class SortedListInterface
定义中。您从成员函数const
的声明中错过了getPosition
限定符。