Deque template class
Mar 14, 2014 at 4:59pm UTC
So I'm trying to make my version of a deck class which has several members like addFirst, addLast, removeFirst, etc... I also want it to be iterable.
So with returnFirst function I'm trying to return the first node of deck but i get several errors:
1 2 3 4 5 6 7 8 9 10 11 12
1>e:\home\deque\deque\deque.h(10):
warning C4346:
'Deque<T>::Node' : dependent name is not a type
1> prefix with 'typename' to indicate a type
1> e:\home\deque\deque\deque.h(32) :
see reference to class template instantiation 'Deque<T>' being compiled
1>e:\home\deque\deque\deque.h(10):
error C2143: syntax error : missing ';' before '*'
1>e:\home\deque\deque\deque.h(10):
error C4430: missing type specifier - int assumed. Note: C++ does not support default -int
1>e:\home\deque\deque\deque.h(10):
fatal error C1903: unable to recover from previous error(s); stopping compilation
This is my header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
Deque.h
template <class T>
class Deque{
public :
Deque();
~Deque();
void addFirst(T item);
void addLast(T item);
void removeFirst();
void removeLast();
Deque<T>::Node * Deque<T>::returnFirst(){
(first == nullptr ) ? throw "Null Pointer Exception: first element" : return first;
} //not sure if it's correct
friend void displayDeque(Deque<T> &deck);
bool isEmpty() const {
return (count == 0);
}
int size() const {
return count;
}
private :
struct Node{
T data;
struct Node *next;
struct Node *previous;
};
Node *first;
Node *last;
int count;
};
template <class T>
Deque<T>::Deque():first=nullptr , last = nullptr , count = 0{
}
template <class T>
Deque<T>::~Deque(){
}
template <class T>
void Deque<T>::addFirst(T item){
if (isEmpty()){
first->data = last->data = item;
first->previous = first->next = last->previous= last->next = nullptr ;
}
else {
Node<T> * newNode = new Node;
newNode->data = item;
first->previous = newNode;
newNode->next = first;
newNode->previous = nullptr ;
first = newNode;
}
}
template <class T>
void displayDeque(Deque<T> deque){
Node<T> *current = first;
if (isEmpty)
cout << "Deck is empty" << endl;
}
and main
1 2 3 4 5 6 7 8
#include "Deque.h"
#include <iostream>
#include <string>
using namespace std;
int main(){
Deque<int > *deck = new Deque<int >();
}
Many thanks to anyone willing to help out a noob :)
Mar 14, 2014 at 5:46pm UTC
'Deque<T>::Node' : dependent name is not a type
1> prefix with 'typename' to indicate a type
typename Deque<T>::Node * Deque<T>::returnFirst(){
Also
1 2
Deque<int > *deck = new Deque<int >();
Deque<int > deck;
Mar 14, 2014 at 5:53pm UTC
Thanks ne555, I've made the changes you suggested and I get :
1 2 3 4
1> e:\home\deque\deque\deque.h(32) :
see reference to class template instantiation 'Deque<T>' being compiled
1>e:\home\deque\deque\deque.h(20):
error C2334: unexpected token(s) preceding '{' ; skipping apparent function body
Mar 14, 2014 at 7:50pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
template <class T>
class Deque{
private :
struct Node; //forward declaration (for returnFirst() )
public :
Deque();
~Deque();
void addFirst(T item);
void addLast(T item);
void removeFirst();
void removeLast();
Node* returnFirst(){ //simple
if (first == nullptr ) //you were abusing the ?: operator
throw "Null Pointer Exception: first element" ;
return first;
}
void displayDeque() const ; //a member function seems more adequate
bool isEmpty() const {
return (count == 0);
}
int size() const {
return count;
}
private :
struct Node{
T data;
struct Node *next;
struct Node *previous;
};
Node *first;
Node *last;
int count;
};
template <class T>
Deque<T>::Deque():first(nullptr ), last(nullptr ), count(0){
}
template <class T>
Deque<T>::~Deque(){
}
template <class T>
void Deque<T>::addFirst(T item){
if (isEmpty()){
first->data = last->data = item;
first->previous = first->next = last->previous= last->next = nullptr ;
}
else {
Node *newNode = new Node; //Node is not a template
newNode->data = item;
first->previous = newNode;
newNode->next = first;
newNode->previous = nullptr ;
first = newNode;
}
}
#include <iostream>
template <class T>
void Deque<T>::displayDeque() const {
Node *current = first;
if (isEmpty)
std::cout << "Deck is empty" << std::endl;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
Deque<int > deck;
}
Mar 14, 2014 at 9:29pm UTC
Big thanks Nee. Basically returnFirst didn't know about the Node structure?
Mar 14, 2014 at 10:03pm UTC
¿why did I bother to comment the changes?
Topic archived. No new replies allowed.