1

Having a lot of trouble getting this program to compile. Here's a list of the full error if that helps:

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>();

I've tried giving them return types in my .cpp file, but that just makes the program think that all my functions are members that need to be pointed to. Does this have something to do with my constructors? I really don't know how to implement those, so they're probably wrong anyway.

The weird thing is that I have the exact same layout for my other class, QueueAsAStack, but I'm not getting any errors for those.

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

I've tried so many things to try and fix it but I just have no idea.

Sam
  • 193
  • 3
  • 11
  • Have you tried writing a non-templated version of the code? –  Nov 24 '17 at 20:59
  • What are the *return types* of these functions? Why haven't you written them for the function definitions? – Some programmer dude Nov 24 '17 at 21:06
  • 2
    I also strongly suggest you read [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Some programmer dude Nov 24 '17 at 21:06
  • `pq` is a pointer so you always need `pq->method()` not `pq.method()`, same for `temp` – Justin Finnerty Nov 24 '17 at 21:08
  • @JustinFinnerty My intention was for pq to be an object of that class. It was supposed to be a priority queue that I did the operations on. – Sam Nov 24 '17 at 21:09
  • Another teacher that tells to implement templates in cpp? I see something like that once per month. This is insane. Put the template in one `.h` file. If you want to separate it, put it in a `.tpp` file. Simple. – Guillaume Racicot Nov 24 '17 at 21:11
  • @GuillaumeRacicot I don't really like it either. I'd rather just make an entire program, and I feel like I'm not learning a lot through this and I'm falling out of practice with other elements of C++ – Sam Nov 24 '17 at 21:15
  • @Sam A good book is priceless. You should get one if you don't feel your teacher is up to date with the latest good practices. However, when you have a question about something in your book, your teacher could be a great reference. – Guillaume Racicot Nov 24 '17 at 21:18

1 Answers1

3

You need to specify the return type. You have:

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

You need:

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

(Note the bool.)

Likewise for the other functions.

Your main issue however, is that you do this:

temp.pop();

temp is a pointer. With pointers, you need to use ->. So:

temp->pop();

Do this in all places you use pq or temp.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • I'll give this another try but those are not included in the .cpp file for LinkedStack provided by my professor. Last time I did try adding them, I got an error that said something like "did you mean pq->peek()?" – Sam Nov 24 '17 at 21:02
  • Yeah I'm getting that error again. Here's one of them : `PriorityQueueHasAStackedQueue.cpp:39:14: error: request for member 'isEmpty' in '((PriorityQueueHasAStackedQueue*)this)->PriorityQueueHasAStackedQueue::temp', which is of pointer type 'PriorityQueueHasAStackedQueue*' (maybe you meant to use '->' ?) while(!temp.isEmpty()){` It has this for all the functions. – Sam Nov 24 '17 at 21:03
  • @Sam Then you should redo your question and ask about that error instead of trying to implement a function that lacks a return type specifier. The function returns `bool`, you can't just omit that. Of course you are going to get an error that tells you that you forgot to specify the return type. – Nikos C. Nov 24 '17 at 21:06
  • I just wasn't sure since the one that was written by my professor doesn't have return types in the cpp file. I assumed that I could follow that same logic. @Nikos C. – Sam Nov 24 '17 at 21:07
  • @Sam A constructor doesn`t have a return type. Maybe your professor implemented a constructor. – Guillaume Racicot Nov 24 '17 at 21:09
  • @Sam Anyway, I updated the answer. Your main issue seems to be that you're using `.` instead of `->` on `temp` and `pq`. They are pointers, so you need `->`. – Nikos C. Nov 24 '17 at 21:10
  • @GuillaumeRacicot You're right, that is my bad. – Sam Nov 24 '17 at 21:11
  • @NikosC. Thank you, I think I get it now. I'm just glad to have a direction to work on. – Sam Nov 24 '17 at 21:12
  • @Sam professors are not always right. In fact, they are often very *wrong*. – Jesper Juhl Nov 24 '17 at 21:16