0

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.

Nick
  • 1
  • 1
  • Do not put `using namespace std;` in a header file. Second, please post the exact error message. – PaulMcKenzie Mar 03 '19 at 07:43
  • This is for an assignment, and using namespace std; was a part of the base code of the file, they don't want it to be changed. – Nick Mar 03 '19 at 07:44
  • This is unbelievable that C++ is now being taught like this. [Have your teacher read this](https://stackoverflow.com/questions/3186226/why-shouldnt-i-put-using-namespace-std-in-a-header). – PaulMcKenzie Mar 03 '19 at 07:45
  • I'll go in and ask, but what about the redefinition error? – Nick Mar 03 '19 at 07:49
  • You need to post a [mcve]. All you posted so far is a header file -- we have no idea how, when, or where you're including this file. Also, `nullptr` has been available since C++ 11. Prefer that over using `NULL` (and thus no need to #include ``) – PaulMcKenzie Mar 03 '19 at 07:51
  • I just updated with all code that I used to test it. Also, Isn't #pragma once an Include guard? – Nick Mar 03 '19 at 07:56
  • `#pragma once` is not portable. The portable include guard has the form of `#ifndef SOMEHEADER_H #define SOMEHEADER_H`, at the very top of the file, and then at the very bottom of the header file `#endif`. Open up one of the standard headers for your compiler, and you should see this form of include guard being used. – PaulMcKenzie Mar 03 '19 at 07:58
  • `Queue.h` and all of those other header files need to have their own include guards. – PaulMcKenzie Mar 03 '19 at 08:00
  • I just did another edit, all of the header files have #pragma once. I'm really confused as to why I get this error. – Nick Mar 03 '19 at 08:02
  • You have other header files you did not post. Second, you could put in `#pragma message ("Node class defined")` inside that header. If you see that message twice in the compiler output window when you compile the main module, then yes, you are including `Node` more than once. – PaulMcKenzie Mar 03 '19 at 08:06
  • I just posted everything. What do you mean by pragma message ("Node class defined")? I've never used anything like that before, I'm very new to c++. Do I just put that right under #pragma once, in the node header file? – Nick Mar 03 '19 at 08:13
  • Yes. Put the `#pragma message` after the `#pragma once`, or better yet, right before the `Node` class gets defined. The `pragma message` can be used for debugging purposes. Since you claim that `Node` is defined only once, this is the proof of whether that is true or not. – PaulMcKenzie Mar 03 '19 at 08:16
  • I just did what you said, Verbatim, this is what the compiler said, "Node.h:17:37: note: #pragma message: Node class defined #pragma message: (Node class defined) – Nick Mar 03 '19 at 08:25
  • @Nick Silly question, but what compiler are you using? Maybe it doesn't understand `#pragma once` it is non-standard. You should be doing things the standard way that Paul McKenzie mentioned. – john Mar 03 '19 at 09:15
  • @Nick So if you see 'Node class defined' twice, isn't that the proof that you are including your header twice and your include guards aren't working? – john Mar 03 '19 at 09:17
  • I'm using Ubuntu, g++ compiler. As for the pragma once thing, I don't know exactly, because it said note: and then repeated it, I think that just comes up once. – Nick Mar 03 '19 at 09:21
  • @Nick -- Ubuntu uses g++, and I am not sure if `#pragma once` is a good idea. I know that `#pragma once` is fully supported with Visual Studio. But that's why there is a fully portable way of implementing it, and that is the way I pointed out (and again, just view one of the compiler's header file, such as `` or `` and you will see this approach). – PaulMcKenzie Mar 03 '19 at 14:10
  • @Nick [Here is an example](https://www.ideone.com/2Wwfqn) – PaulMcKenzie Mar 03 '19 at 14:17

0 Answers0