原型for与班级

时间:2017-11-24 20:56:35

标签: c++

在编译这个程序时遇到很多麻烦。如果有帮助,这里是完整错误的列表:

PriorityQueueHasAStackedQueue.cpp:16:52: error: ISO C++ forbids declaration of 'isEmpty' with no type [-fpermissive]
 PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
                                                    ^
PriorityQueueHasAStackedQueue.cpp:16:1: error: prototype for 'int PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const' does not match any in class 'PriorityQueueHasAStackedQueue<ItemType>'
 PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
 ^
In file included from PriorityQueueHasAStackedQueue.cpp:6:0,
                 from project4.cpp:9:
PriorityQueueHasAStackedQueue.h:22:7: error: candidate is: bool PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
  bool isEmpty() const;
       ^
In file included from project4.cpp:9:0:
PriorityQueueHasAStackedQueue.cpp:22:74: error: ISO C++ forbids declaration of 'enqueue' with no type [-fpermissive]
 PriorityQueueHasAStackedQueue<ItemType>::enqueue(const ItemType& newEntry)
                                                                          ^
PriorityQueueHasAStackedQueue.cpp:22:1: error: prototype for 'int PriorityQueueHasAStackedQueue<ItemType>::enqueue(const ItemType&)' does not match any in class 'PriorityQueueHasAStackedQueue<ItemType>'
 PriorityQueueHasAStackedQueue<ItemType>::enqueue(const ItemType& newEntry)
 ^
In file included from PriorityQueueHasAStackedQueue.cpp:6:0,
                 from project4.cpp:9:
PriorityQueueHasAStackedQueue.h:23:7: error: candidate is: bool PriorityQueueHasAStackedQueue<ItemType>::enqueue(const ItemType&)
  bool enqueue(const ItemType& newEntry);
       ^
In file included from project4.cpp:9:0:
PriorityQueueHasAStackedQueue.cpp:30:50: error: ISO C++ forbids declaration of 'dequeue' with no type [-fpermissive]
 PriorityQueueHasAStackedQueue<ItemType>::dequeue()
                                                  ^
PriorityQueueHasAStackedQueue.cpp:30:1: error: prototype for 'int PriorityQueueHasAStackedQueue<ItemType>::dequeue()' does not match any in class 'PriorityQueueHasAStackedQueue<ItemType>'
 PriorityQueueHasAStackedQueue<ItemType>::dequeue()
 ^
In file included from PriorityQueueHasAStackedQueue.cpp:6:0,
                 from project4.cpp:9:
PriorityQueueHasAStackedQueue.h:24:7: error: candidate is: bool PriorityQueueHasAStackedQueue<ItemType>::dequeue()
  bool dequeue();
       ^
In file included from project4.cpp:9:0:
PriorityQueueHasAStackedQueue.cpp:48:49: error: ISO C++ forbids declaration of 'peek' with no type [-fpermissive]
 PriorityQueueHasAStackedQueue<ItemType>::peek() const
                                                 ^
PriorityQueueHasAStackedQueue.cpp:48:1: error: prototype for 'int PriorityQueueHasAStackedQueue<ItemType>::peek() const' does not match any in class 'PriorityQueueHasAStackedQueue<ItemType>'
 PriorityQueueHasAStackedQueue<ItemType>::peek() const
 ^
In file included from PriorityQueueHasAStackedQueue.cpp:6:0,
                 from project4.cpp:9:
PriorityQueueHasAStackedQueue.h:25:11: error: candidate is: ItemType PriorityQueueHasAStackedQueue<ItemType>::peek() const
  ItemType peek() const;
           ^
PriorityQueueHasAStackedQueue.h: In function 'void testPriorityQueue(int*, int)':
PriorityQueueHasAStackedQueue.h:21:2: error: 'PriorityQueueHasAStackedQueue<ItemType>::PriorityQueueHasAStackedQueue() [with ItemType = int]' is private
  PriorityQueueHasAStackedQueue();
  ^
project4.cpp:40:79: error: within this context
  PriorityQueueInterface<int>* pQueue = new PriorityQueueHasAStackedQueue<int>();

我试过在我的.cpp文件中给它们返回类型,但这只是让程序认为我的所有函数都是需要指向的成员。这与我的构造函数有关吗?我真的不知道如何实现这些,所以无论如何它们可能都是错的。

奇怪的是,我的其他类QueueAsAStack的布局完全相同,但我没有收到任何错误。

PriorityQueueHasAStackedQueue.h:

#ifndef PRIORITY_QUEUE_HAS_A_STACKED_QUEUE_
#define PRIORITY_QUEUE_HAS_A_STACKED_QUEUE_

#include "PriorityQueueInterface.h"
#include "QueueAsAStack.h"
#include "LinkedStack.h"

template<class ItemType>
class PriorityQueueHasAStackedQueue : public PriorityQueueInterface<ItemType>
{
//private:
    PriorityQueueHasAStackedQueue<int>* pq = new PriorityQueueHasAStackedQueue<int>();
    PriorityQueueHasAStackedQueue<int>* temp = new PriorityQueueHasAStackedQueue<int>();

//public:
    PriorityQueueHasAStackedQueue();
    bool isEmpty() const;
    bool enqueue(const ItemType& newEntry);
    bool dequeue();
    ItemType peek() const;

};

#endif

PriorityQueueHasAStackedQueue.cpp:

#include "PriorityQueueHasAStackedQueue.h"
#include "PriorityQueueInterface.h"
#include "LinkedStack.h"

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::PriorityQueueHasAStackedQueue()
{
}

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
{
    return pq.peek() == nullptr;
}

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::enqueue(const ItemType& newEntry)
{
    pq.push(newEntry);

    return true;
}

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::dequeue()
{
    while(!pq.isEmpty()){
        temp.push(pq.peek());
        pq.pop();
    }

    temp.pop();

    while(!temp.isEmpty()){
        pq.push(temp.peek());
        temp.pop();
    }

    return true;
}

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::peek() const
{
    PriorityQueueHasAStackedQueue<int> peekedItem;

    while(!pq.isEmpty()){
        temp.push(pq.peek());
        pq.pop();
    }

    peekedItem = temp.peek();

    while(!temp.isEmpty()){
        pq.push(temp.peek());
        temp.pop();
    }

    return peekedItem;
}

PriorityQueueInterface.h:

#ifndef PRIORITY_QUEUE_INTERFACE_
#define PRIORITY_QUEUE_INTERFACE_

template<class ItemType>
class PriorityQueueInterface

{
public:
   /** Sees whether this priority queue is empty.
    @return  True if the priority queue is empty, or false if not. */
   virtual bool isEmpty() const = 0;

   /** Adds a new entry to this priority queue.
    @post  If the operation was successful, newEntry is in the
       priority queue.
    @param newEntry  The object to be added as a new entry.
    @return  True if the addition is successful or false if not. */
   virtual bool enqueue(const ItemType& newEntry) = 0;

   /** Removes from this priority queue the entry having the 
       highest priority.
    @post  If the operation was successful, the highest priority 
       entry has been removed.
    @return  True if the removal is successful or false if not. */
   virtual bool dequeue() = 0;

   /** Returns the highest-priority entry in this priority queue.
    @pre  The priority queue is not empty.
    @post  The highest-priority entry has been returned, and the
       priority queue is unchanged.
    @return  The highest-priority entry. */
   virtual ItemType peek() const = 0;

   /** Destroys object and frees memory allocated by object. */
   virtual ~PriorityQueueInterface() { }
}; // end PriorityQueueInterface
#endif

我已经尝试了很多东西来尝试解决它,但我只是不知道。

1 个答案:

答案 0 :(得分:3)

您需要指定返回类型。你有:

template<class ItemType>
PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
{
    return pq.peek() == nullptr;
}

你需要:

template<class ItemType>
bool PriorityQueueHasAStackedQueue<ItemType>::isEmpty() const
{
    return pq.peek() == nullptr;
}

(注意bool。)

同样适用于其他功能。

然而,您的主要问题是您这样做:

temp.pop();

temp是一个指针。使用指针,您需要使用->。所以:

temp->pop();

在您使用pqtemp的所有地方执行此操作。