2014-11-09 51 views
-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 

任何幫助和建議表示讚賞!

+1

編譯器說的是「嘿,夥計,'類SortedListHasA'是純抽象的,我不能創建這個類的對象」 – 101010 2014-11-09 01:01:34

+0

,但類SortedListHasA不是純虛擬類 – 2014-11-09 01:04:16

+0

編譯器明確指出'虛擬int getPosition(const ItemType&anEntry)= 0;'getPosition'聲明中的'''= 0'聲明class SortedListHasA'爲純虛函數。 – 101010 2014-11-09 01:05:40

回答

2

清理OK事情只是更改爲:

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

class SortedListInterface定義。您從成員函數getPosition的聲明中遺漏const限定符。

+0

opps !!非常感謝 – 2014-11-09 01:25:03

相關問題