Trying to write a simple linked-list queue. It worked before I split it into a header and a cpp file. I'm recently getting back to programming (in cpp) and I must have the syntax for this wrong somewhere. Any help is appreciated!
#pragma once
#ifndef QUEUE_H
#define QUEUE_H
#ifndef NULL
#define NULL 0
//implement a Queue using linked nodes
usingnamespace std;
template <typename ItemType>
class Queue //holds items in a first-in, first out linked list
{
private:
//Nodes are the basic element in the queue, holding an item and a pointer to the next item
struct Node {
ItemType item;
Node* next;
};
Node* head;
Node* tail;
public:
Queue():head(NULL),tail(NULL){}
bool isEmpty(); //returns true if Queue is empty, false if not
void add(const ItemType& item); //adds an item to the queue is the last position
ItemType remove(); //removes an item from the queue and dereferences the pointer
~Queue();
};
#endif
#endif
#include "Queue.h"
bool Queue::isEmpty(){
return head == NULL;
}
void Queue::add(const ItemType& item) {
Node* n = new Node();
n->item = item;
if(isEmpty())
head = n;
else
tail->next=n;
tail = n;
}
ItemType Queue::remove() {
Node* n = head;
ItemType item = n->item;
head = head->next;
delete n;
return item;
}
Queue::~Queue() {
while(!isEmpty()) remove();
}
Here's what the compiler tells me:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Queue.cpp:3:6: error: ‘template<class ItemType> class Queue’ used without template parameters
Queue.cpp: In function ‘bool isEmpty()’:
Queue.cpp:4:9: error: ‘head’ was not declared in this scope
Queue.cpp: At global scope:
Queue.cpp:7:6: error: ‘template<class ItemType> class Queue’ used without template parameters
Queue.cpp:7:23: error: ‘ItemType’ does not name a type
Queue.cpp:7:33: error: ISO C++ forbids declaration of ‘item’ with no type [-fpermissive]
Queue.cpp: In function ‘void add(constint&)’:
Queue.cpp:8:2: error: ‘Node’ was not declared in this scope
Queue.cpp:8:8: error: ‘n’ was not declared in this scope
Queue.cpp:8:16: error: expected type-specifier before ‘Node’
Queue.cpp:8:16: error: expected ‘;’ before ‘Node’
Queue.cpp:11:3: error: ‘head’ was not declared in this scope
Queue.cpp:13:3: error: ‘tail’ was not declared in this scope
Queue.cpp:14:2: error: ‘tail’ was not declared in this scope
Queue.cpp: At global scope:
Queue.cpp:17:1: error: ‘ItemType’ does not name a type
Queue.cpp:25:1: error: invalid use of template-name ‘Queue’ without an argument list
It looks like my Queue class just isn't being defined for some reason. I've just spent a lot of time trying to figure out why and I can't figure it out.
Any help is appreciated!