2016-05-24 8 views
-6

У меня есть не может создать экземпляр абстрактного класса, и мне кажется, что он не нашел, почему он дал мне ошибку. Класс вообще не абстрактный. Я провел некоторое исследование, но не могу найти никакого света. Надеюсь, кто-нибудь может мне помочь.C++ не может создать экземпляр абстрактного класса

Вот часть кода, вам, ребята, нужно больше, чем я могу предоставить.

#include <cassert> 
    #include "PriorityQueueInterface.h" 
    #include "LinkedSortedList.h" 
    #include "PrecondViolatedExcep.h" 

    template<class ItemType> 
    class SL_PriorityQueue : public PriorityQueueInterface<ItemType> 
    { 
    private: 
     LinkedSortedList<ItemType>* slistPtr; // Pointer to sorted list of 
               // items in the priority queue 

    public: 
     SL_PriorityQueue(); 
     SL_PriorityQueue(const SL_PriorityQueue& pq); 
     ~SL_PriorityQueue(); 

     bool isEmpty() const; 
     bool add(const ItemType& newEntry); 
     bool remove(); 

     /** @throw PrecondViolatedExcep if priority queue is empty. */ 
     ItemType peek() const throw(PrecondViolatedExcep); 
    }; // end SL_PriorityQueue 

    template<class ItemType> 
    SL_PriorityQueue<ItemType>::SL_PriorityQueue() 
    { 
     slistPtr = new LinkedSortedList<ItemType>(); //Here is the error 
    } // end default constructor 

    template< class ItemType> 
    SL_PriorityQueue<ItemType>::SL_PriorityQueue(const SL_PriorityQueue& pq) : 
     listPtr(pq.listPtr) 
    { 
    } // end copy constructor 

    template<class ItemType> 
    SL_PriorityQueue<ItemType>::~SL_PriorityQueue() 
    { 
    } // end destructor 


    template< class ItemType> 
    bool SL_PriorityQueue<ItemType>::add(const ItemType& newEntry) 
    { 
     slistPtr->insertSorted(newEntry); 
     return true; 
    } // end add 
    template< class ItemType> 
    bool SL_PriorityQueue<ItemType>::remove() 
    { 
     // The highest-priority item is at the end of the sorted list 
     return slistPtr->remove(slistPtr->getLength()); 
    } // end remove 

    template< class ItemType> 
    bool SL_PriorityQueue<ItemType>::isEmpty() const 
    { 
     return slistPtr->isEmpty(); 
    } // end isEmpty 

    template<class ItemType> 
    ItemType SL_PriorityQueue<ItemType>::peek() const throw(PrecondViolatedExcep) 
    { 
     if (isEmpty()) 
      throw PrecondViolatedExcep("peekFront() called with empty queue."); 

     // Priority queue is not empty; return highest priority item; 
     // it is at the end of the sorted list 
     return slistPtr->getEntry(slistPtr->getLength()); 
    } // end peek 

Это главный

#include <iostream> 
#include "SL_PriorityQueue.h" 

using namespace std; 

int main() { 

    SL_PriorityQueue<int> queue; 

    queue.add(2); 

    return 0; 
} 

Ошибка в следующей строке класса SL_PriorityQueue

template<class ItemType> 
SL_PriorityQueue<ItemType>::SL_PriorityQueue() 
{ 
    slistPtr = new LinkedSortedList<ItemType>(); 
} // end default constructor 

Наконец, LinkedSortedList в случае, если кто-то нуждается.

#include "SortedListInterface.h" 
#include "Node.h" 
#include "PrecondViolatedExcep.h" 

template<class ItemType> 
class LinkedSortedList : public SortedListInterface<ItemType> 
{ 
private: 
    Node<ItemType>* headPtr; // Pointer to first node in the chain 
    int itemCount;   // Current count of list items 

          // Locates the node that is before the node that should or does 
          // contain the given entry. 
          // @param anEntry The entry to find. 
          // @return Either a pointer to the node before the node that contains 
          // or should contain the given entry, or nullptr if no prior node exists. 
    Node<ItemType>* getNodeBefore(const ItemType& anEntry) const; 

    // Locates the node at a given position within the chain. 
    Node<ItemType>* getNodeAt(int position) const; 

    // Returns a pointer to a copy of the chain to which origChainPtr points. 
    Node<ItemType>* copyChain(const Node<ItemType>* origChainPtr); 

public: 
    LinkedSortedList(); 
    LinkedSortedList(const LinkedSortedList<ItemType>& aList); 
    virtual ~LinkedSortedList(); 

    void insertSorted(const ItemType& newEntry); 
    bool removeSorted(const ItemType& anEntry); 
    int getPosition(const ItemType& newEntry) const; 

    // The following methods are the same as given in ListInterface: 
    bool isEmpty() const; 
    int getLength() const; 
    bool remove(int position); 
    void clear(); 
    ItemType getEntry(int position) const throw(PrecondViolatedExcep); 
}; // end LinkedSortedList 

template<class ItemType> 
LinkedSortedList<ItemType>::LinkedSortedList() : headPtr(nullptr), itemCount(0) 
{ 
} // end default constructor 

template<class ItemType> 
LinkedSortedList<ItemType>::LinkedSortedList(const LinkedSortedList<ItemType>& aList) 
{ 
    headPtr = copyChain(aList.headPtr); 
} // end copy constructor 

template<class ItemType> 
LinkedSortedList<ItemType>::~LinkedSortedList() 
{ 
    clear(); 
} // end destructor 

template< class ItemType> 
void LinkedSortedList<ItemType>::clear() 
{ 
    while (!isEmpty()) 
     remove(1); 
} // end clear 


template< class ItemType> 
ItemType LinkedSortedList<ItemType>::getEntry(int position) const 
throw(PrecondViolatedExcep) 
{ 
    return LinkedSortedList<ItemType>::getEntry(position); 
} // end getEntry 

template< class ItemType> 
bool LinkedSortedList<ItemType>::remove(int position) 
{ 
    bool ableToRemove = (position >= 1) && (position <= itemCount); 
    if (ableToRemove) 
    { 
     Node<ItemType>* curPtr = nullptr; 
     if (position == 1) 
     { 
      // Remove the first node in the chain 
      curPtr = headPtr; // Save pointer to node 
      headPtr = headPtr->getNext(); 
     } 
     else 
     { 
      // Find node that is before the one to delete 
      Node<ItemType>* prevPtr = getNodeAt(position - 1); 
      // Point to node to delete 
      curPtr = prevPtr->getNext(); 
      // Disconnect indicated node from chain by connecting the 
      // prior node with the one after 
      prevPtr->setNext(curPtr->getNext()); 
     } // end if 
      // Return node to system 
     curPtr->setNext(nullptr); 

     delete curPtr; 
     curPtr = nullptr; 
     itemCount-- ; // Decrease count of entries 
    } // end if 
    return ableToRemove; 
} // end remove 

template< class ItemType> 
bool LinkedSortedList<ItemType>::isEmpty() const { 

    return itemCount == 0; 
} 

template< class ItemType> 
int LinkedSortedList<ItemType>::getLength() const { 

    return itemCount; 
} 


template< class ItemType> 
bool LinkedSortedList<ItemType>::removeSorted(const ItemType& anEntry) 
{ 
    bool ableToRemove = false; 
    if (!LinkedSortedList<ItemType>::isEmpty()) 
    { 
     int position = getPosition(anEntry); 
     ableToRemove = position > 0; 
     if (ableToRemove) 
      ableToRemove = LinkedSortedList<ItemType>::remove(position); 
    } // end if 
    return ableToRemove; 
} // end removeSorted 


template< class ItemType> 
int LinkedSortedList<ItemType>::getPosition(const ItemType& anEntry) const 
{ 
    int position = 1; 
    int length = LinkedSortedList<ItemType>::getLength(); 
    while ((position <= length) && 
     (anEntry > LinkedSortedList<ItemType>::getEntry(position))) 
    { 
     position++; 
    } // end while 
    if ((position > length) || 
     (anEntry != LinkedSortedList<ItemType>::getEntry(position))) 
    { 
     position = -position; 
    } // end if 
    return position; 
} // end getPosition 





template<class ItemType> 
void LinkedSortedList<ItemType>::insertSorted(const ItemType& newEntry) 
{ 
    Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry); 
    Node<ItemType>* prevPtr = getNodeBefore(newEntry); 

    if (isEmpty() || (prevPtr == nullptr)) // Add at beginning 
    { 
     newNodePtr->setNext(headPtr); 
     headPtr = newNodePtr; 
    } 
    else // Add after node before 
    { 
     Node<ItemType>* aftPtr = prevPtr->getNext(); 
     newNodePtr->setNext(aftPtr); 
     prevPtr->setNext(newNodePtr); 
    } // end if 

    itemCount++; 
} // end insertSorted 

    // Private Methods: 

template<class ItemType> 
Node<ItemType>* LinkedSortedList<ItemType>::copyChain(const Node<ItemType>* origChainPtr) 
{ 
    Node<ItemType>* copiedChainPtr; 
    if (origChainPtr == nullptr) 
    { 
     copiedChainPtr = nullptr; 
    } 
    else 
    { 
     // Build new chain from given one 
     copiedChainPtr = new Node<ItemType>(origChainPtr->getItem()); 
     copiedChainPtr->setNext(copyChain(origChainPtr->getNext())); 
    } // end if 

    return copiedChainPtr; 
} // end copyChain 

template<class ItemType> 
Node<ItemType>* LinkedSortedList<ItemType>::getNodeBefore(const ItemType& anEntry) const 
{ 
    Node<ItemType>* curPtr = headPtr; 
    Node<ItemType>* prevPtr = nullptr; 

    while ((curPtr != nullptr) && (anEntry > curPtr->getItem())) 
    { 
     prevPtr = curPtr; 
     curPtr = curPtr->getNext(); 
    } // end while 

    return prevPtr; 
} // end getNodeBefore 

template< class ItemType> 
Node<ItemType>* LinkedSortedList<ItemType>::getNodeAt(int position) const 
{ 
    // Debugging check of precondition 
    assert((position >= 1) && (position <= itemCount)); 
    // Count from the beginning of the chain 
    Node<ItemType>* curPtr = headPtr; 
    for (int skip = 1; skip < position; skip++) 
     curPtr = curPtr->getNext(); 
    return curPtr; 
} // end getNodeAt 

EDIT: код SortedListInterface добавил

#pragma once 
template<class ItemType> 
class SortedListInterface 
{ 
public: 

    virtual void insertSorted(const ItemType& newEntry) = 0; 


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


    negative integer. */ 
    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; 
}; 
+0

Возможно, LinkedSortedList <> не реализует все чистые виртуальные методы SortedListInterface <>, но без объявления SortedListInterface <> мы не можем сказать. –

+0

Я обновлю вопрос с помощью интерфейса – Edgar

+0

Что такое определение PriorityQueueInterface - есть ли у вас абстрактная функция, которую вы не реализовали? Какова полная ошибка? – kfsone

ответ

2

getPosition является неконстантным в классе интерфейса, но сопзИте в производном классе. Это две разные функции и вызывают проблемы.

Добавление ключевого слова override в класс реализации (если ваш компилятор поддерживает его) будет отмечать эту проблему.

+0

Спасибо. Во всем этом коде я потерялся и пропустил его. – Edgar