C++不能实例化抽象类

C++不能实例化抽象类,c++,queue,C++,Queue,我不能实例化抽象类,我似乎不知道为什么它会给我错误。这个类根本不是抽象的。我做了一些研究,但找不到任何线索。我希望有人能帮助我 这是代码的一部分,你们需要更多我能提供的 #include <cassert> #include "PriorityQueueInterface.h" #include "LinkedSortedList.h" #include "PrecondViolatedExcep.h" template<class Item

我不能实例化抽象类,我似乎不知道为什么它会给我错误。这个类根本不是抽象的。我做了一些研究,但找不到任何线索。我希望有人能帮助我

这是代码的一部分,你们需要更多我能提供的

 #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 
编辑: 已添加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;
}; 
getPosition在接口类中为非常量,但在派生类中为常量。这是两种不同的功能,会导致您的问题


如果编译器支持,在实现类中添加override关键字将标记此类问题。

可能LinkedSortedList没有实现SortedListInterface的所有纯虚拟方法,但没有SortedListInterface的声明,我们无法确定。我将用接口更新问题定义是什么PriorityQueueInterface-是否有您尚未实现的抽象函数?完全错误是什么?好了-您必须实现SortedListInterface中所有具有=0的函数-这些函数使其变得抽象。使用override可帮助您检测此类错误。