1

I am trying to implement a DEQUE using double linked list.

DEQUE.h

using namespace std;

template <typename T>
class Node{
    Node(const T& data):data(data), next(0), prev(0) {}
    public:
        Node* next;
        Node* prev;
        T data;
};

template <typename T>
class DEQUE
{

//interface
};

DEQUE.cpp

template <class T>
void DEQUE< T > ::AddFirst(T t){
    Node<T>* temp = new Node(t);
    if ( counter != 0 ) {
        temp->next = head;
        temp->prev = 0 ;
        head->prev = temp;
        head =temp;
        counter++;
    }

    else{
        head = temp;
        tail = temp;
        temp->next = 0;
        temp->prev = 0;
        counter++;
    }
};

I am getting expected type-specifier before 'Node' error on the line

Node<T>* temp = new Node(t);

what am I doing wrong here? Thanks for the help in advance.

  • You are missing `#include "DEQUE.h"` – Drew Dormann May 05 '15 at 03:50
  • Somewhat related: It is *highly* likely that template implementation should be moved to your deque.h header file. [Read this for why.](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file?s=2|2.6716). – WhozCraig May 05 '15 at 03:52
  • @DrewDormann- DEQUE.h is there, I just showed the problematic part of the code. – orthogonal_tangent May 05 '15 at 03:54
  • @WhozCraig - I know its not the portable way, but i am including the cpp file path in the main file and its working fine except for the error above, another way is to include DEQUE.cpp in the DEQUE.h so that when DEQUE.h is included, so is DEQUE.cpp. – orthogonal_tangent May 05 '15 at 03:57
  • @user3259345 Portability has nothing to do with it.You could split it up countless ways, but *why*? People see a .cpp file, they expect a *translation unit*. If you want to go from Trenton to New York, you can certainly go by way of LA, Moscow, and Heathrow I suppose, but *why*. Anyway, best of luck. – WhozCraig May 05 '15 at 04:03
  • @WhozCraig - What you are saying makes complete sense. Since I am knew at this, I am into learning what all can work besides what works best. Thanks for the input, best of luck to you too. – orthogonal_tangent May 05 '15 at 18:10

1 Answers1

2

You forgot the type when creating an instance of Node:

Node<T>* temp = new Node<T>(t);
                        ^^^  missing.

The type used to create an instance of Node is not automatically assumed to be same as the type used for DEQUE. You have to explicitly specify it.

R Sahu
  • 204,454
  • 14
  • 159
  • 270