制作模板化节点类时重新定义错误

时间:2019-03-03 07:41:11

标签: c++ templates linked-list nodes

当我在入队函数中调用该重定义错误时,我正在处理此代码。我到处都在寻找如何解决它,但无济于事。我使用过标题保护程序,但我认为我没有在其他任何地方重新定义它,说实话,我很固守在这里。非常感谢您的帮助。这与我已经查找的情况有些不同,因为他们没有使用模板。

#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。

好的,现在一切都在里面,我只是觉得它会变得很混乱。

0 个答案:

没有答案