why i cannot create a class object?
Oct 8, 2015 at 6:53am UTC
I am working on an assignment regarding doubly linked lists... i have to write a driver program but i am not able to create an object of dllist class..plz help
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include<iostream.h>
#include<conio.h>
template <class T>
class dlnode
{
public :
T info;
dlnode *next,*previous;
dlnode()
{
next=previous=0;
}
dlnode(const T& el,dlnode *nxt=0,dlnode *prev=0)
{
info=el;
next=nxt;
previous=prev;
}
};
template <class T>
class dllist
{
protected :
dlnode<T> *head,*tail;
public :
dllist()
{
head=tail=0;
}
bool isempty(dllist<T>);
void display();
void insertinfront(const T&);
void insertinend(const T&);
void insertafter(const T&,T x);
void deletefromfront();
void deletefromend();
void findvalue(const T&);
void deletevalue2ndvalue(const T&);
void movenode(const T&,int y);
int countnode(dllist<T>);
};
template <class T>
dllist<T> obj; // <----------- here, why it's not creating object
template <class T>
bool dllist<T>::isempty(dllist x) // IS EMPTY??
{
if (x->head=0)
return true ;
else
return false ;
}
template <class T>
void dllist<T>::insertinend(const T& el) //INSERT IN END
{
if (head==0)
{
head=tail=new dlnode<T>(el);
}
else
{
tail=new dlnode<T>(el,0,tail);
tail->previous->next=tail;
}
}
template <class T>
void dllist<T>::insertinfront(const T& el) //INSERT IN FRONT
{ dlnode<T> *temp;
if (head==0)
{
head=tail=new dlnode<T>(el);
}
else
{
head->previous=new dlnode<T>(el,head,0);
}
}
int main()
{
return 0;
}
Oct 8, 2015 at 7:12am UTC
The template keyword can only be applied to classes and functions, not to objects. If you want to create an instance of a dllist<T>, you have to tell the compiler the actual value of T. E.g.
You should avoid putting an instance in global scope in this case. You don't need it.
Last edited on Oct 8, 2015 at 7:12am UTC
Oct 8, 2015 at 7:20am UTC
thanks brother
Topic archived. No new replies allowed.