Ошибка переопределения при создании класса Templated Node - PullRequest
0 голосов
/ 03 марта 2019

Я работал над этим кодом, когда натолкнулся на эту ошибку переопределения, когда вызвал ее в своей функции постановки в очередь.Я везде искал, как это исправить, но безрезультатно.Я использовал защиту заголовка, и я не думаю, что переопределил ее где-либо еще, я честно застрял здесь.Помощь очень ценится.Это немного отличается от случаев, которые я уже просматривал, поскольку они не используют шаблоны.

#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 */

Редактировать: Queue.h, Node.h, PriorityQueue.h и Event.h все имеют #pragma один раз.

Хорошо, ВСЕ сейчас там, я просто чувствовал, что это будетполучить действительно беспорядок.

...