我无法实例化抽象类,我似乎无法找到它给我错误的原因。这个课程根本不是抽象的。我做了一些研究,但找不到任何光。我希望有人可以帮我。
以下是代码的一部分,你们需要更多我能提供的代码。
#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;
};
答案 0 :(得分:2)
override
在接口类中是非const的,但在派生类中是const。这是两个不同的功能,并导致您的问题。
在实现类中添加ConnectionChangeReceiver.onReceive()
关键字(如果您的编译器支持它)将标记此类问题。