I was working on this code, when I came across this redefinition error when I called it in my enqueue function. I've looked everywhere on how to fix it, but to no avail. I've used the header guard and I don't think I've redefined it anywhere else, I'm honestly pretty stuck here. Help is greatly appreciated. This is a little bit different from the cases I've looked up already, since they're not using templates.
#pragma once
#include <cstdio> // Needed for NULL
using namespace std;
template <class T>
class Node { // this is where the error happens, says "redefinition of class Node<T>" here, adds in a note that there's a previous definition of class Node<T> here.
public:
Node<T>* next;
T data;
Node();
Node(T dat);
}; // Node
template <class T>
Node<T>::Node(){
next = NULL;
}
template <class T>
Node<T>::Node(T thedata){
next = NULL;
data = thedata
}
//PriorityQueue.h
//There is a #pragma once at the top of the file.
template <class T>
void PriorityQueue<T>::enqueue(const T& x){
Node<T>* tempN;
Node<T>* newN = new Node<T>(x);
if(head == NULL || newN < head){
head = newN;
head->next = NULL;
}
else{
tempN = head;
while(tempN->next != NULL && tempN < newN){
tempN = tempN->next;
}
newN->next = tempN->next;
tempN->next = newN;
}
}
template <class T>
T& PriorityQueue<T>::peek() const throw(EmptyDataCollectionException){
return head->data;
}
#include "Queue.h"
#include "Event.h"
#include "PriorityQueue.h"
#include "EmptyDataCollectionException.h"
#include <iostream>
using namespace std;
int main () {
Event x;
Event x1;
Event x2;
//type 0 is arrival, type 1 is departure.
x.setLength(10);
x.setArrivaltime(5);
x1.setLength(4);
x1.setArrivaltime(3);
x2.setLength(4);
x2.setArrivaltime(8);
PriorityQueue<Event> Q;
Q.enqueue(x);
Q.enqueue(x1);
Q.enqueue(x2);
Event y = Q.peek();
cout << endl << "top: " << y;
return 0;
} // main
#pragma once
using namespace std;
typedef enum etype { arrival, departure } EventType;
// Desc: Represents a simulation event
class Event {
private:
EventType type;
unsigned arrivaltime;
// standing for arrival time.
unsigned length;
public:
//the default constructor for event.
Event();
EventType getType() const;
unsigned getArrivaltime() const;
unsigned getLength() const;
void setType(const EventType atype);
void setArrivaltime(const unsigned AnArrivaltime);
void setLength(const unsigned ALength);
// Desc: Comparators
bool operator<(const Event &r);
bool operator>(const Event &r);
bool operator<=(const Event &r);
bool operator>=(const Event &r);
friend ostream & operator<<(ostream & os, const Event & r);
}; // Event
#pragma once
#include "EmptyDataCollectionException.h"
#include <cstdio>
using namespace std;
template <class T>
class Queue {
private:
static unsigned const INITIAL_SIZE = 6;
int * arr;
unsigned size;
unsigned capacity;
unsigned frontindex;
unsigned backindex;
/* you choose your implementation */
Edit: Queue.h, Node.h, PriorityQueue.h and Event.h all have #pragma once.
Okay, EVERYTHING is in there now, I just felt like it would get really cluttered.