| | |
Template specification - Constructors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Hi all.
I am trying to write a template class queue for an "assignment" and I got stuck by what I think is a syntax error.
To be honest, the assignment was MUCH easier than that but after having it completed I thought to be interesting to explore new possibilities... here's why the " "
I have two template structures, slist and dlist, that are in fact nodes of single linked lists and double linked lists respectively, that can store data of any given type.
I have a template class, queue, in order to create queues with data organized either in Single or Double linked lists.
I am trying to write two different constructors, one to be run if the object is a queue based on a SLL data structure and one for object built upon the other struct. The reason is that I most likely will need to hande more specifically one or the other case, (e.g. the second pointer of the dll). It would be nice to understand the concept in order to differentiate some other funcion, too, but I bet that this constructor case will give me the start.
Here's what I have so far:
How may I differentiate the constructors?
This code isn't compiling for the following reason, that I don't understand.
Thanks in advance for any help, and as usual sorry for my poor english, I Hope I explained myself anyway ^^"
I am trying to write a template class queue for an "assignment" and I got stuck by what I think is a syntax error.
To be honest, the assignment was MUCH easier than that but after having it completed I thought to be interesting to explore new possibilities... here's why the " "

I have two template structures, slist and dlist, that are in fact nodes of single linked lists and double linked lists respectively, that can store data of any given type.
I have a template class, queue, in order to create queues with data organized either in Single or Double linked lists.
I am trying to write two different constructors, one to be run if the object is a queue based on a SLL data structure and one for object built upon the other struct. The reason is that I most likely will need to hande more specifically one or the other case, (e.g. the second pointer of the dll). It would be nice to understand the concept in order to differentiate some other funcion, too, but I bet that this constructor case will give me the start.
Here's what I have so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string.h> using std::cout; using std::endl; using std::cin; template <class D> struct slist { public: D data; slist<D> *next; }; template <class D> struct dlist { public: D data; dlist<D> *next; dlist<D> *prev; }; template <class S> class queue { S *start; string name; public: queue(void); ~queue(void); }; template <class D> queue<slist<D> >::queue(void) { start = new slist<D>; cout << endl << "Constructor slist Test" << endl; return; } template <class D> queue<dlist<D> >::queue(void) { start = new dlist<D>: cout << endl << "Constructor dlist Test" << endl; return; } template <class S> queue<S>::~queue(void) { delete start; cout << endl << "Destructor Test" << endl; return; } int main(void) { queue<slist<int> > ob1; cin.get(); return EXIT_SUCCESS; }
How may I differentiate the constructors?
This code isn't compiling for the following reason, that I don't understand.
C++ Syntax (Toggle Plain Text)
error: invalid use of incomplete type ‘class queue<slist<D> >’ error: declaration of ‘class queue<slist<D> >’ error: invalid use of incomplete type ‘class queue<dlist<D> >’ error: declaration of ‘class queue<dlist<D> >’
Thanks in advance for any help, and as usual sorry for my poor english, I Hope I explained myself anyway ^^"
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
Something like this might work. But I think you would be better off defining your nodes as classes instead of structures. It will give you encapsulation for your data.
C++ Syntax (Toggle Plain Text)
template <class T> struct slist { public: T Data; slist<T> *next; }; template <class T> struct Dlist { public: T Data; Dlist<T> *next; Dlist<T> *prev; }; template <class T> class myQueue{ public: myQueue<T>(); ~myQueue<T>(void); private: T *start; }; template <class T> myQueue<T>::myQueue() { start = new T; return; } template <class T> myQueue<T>::~myQueue(void) { delete start; return; } int main() { myQueue<struct slist<int>> sobj; myQueue<struct Dlist<int>> dobj; cin.get(); return EXIT_SUCCESS; }
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Got the hint about using classes, I'm making the modifications 
However, my problem was of another kind ^^"
I can create two objects of type myQueue, each based on a different data structure. That's fine, but how can I make these objects call for differents constructors depending on that structures?
I mean, if in
I add the instruction "prev = NULL;", what will happen when the constructor is called for an object of type, say, myQueue<slist<int> > ?
Should I put specific and structure-depending instructions in the constructors of the nodes classes?

However, my problem was of another kind ^^"
I can create two objects of type myQueue, each based on a different data structure. That's fine, but how can I make these objects call for differents constructors depending on that structures?
I mean, if in
C++ Syntax (Toggle Plain Text)
template <class T> myQueue<T>::myQueue() { start = new T; return; }
I add the instruction "prev = NULL;", what will happen when the constructor is called for an object of type, say, myQueue<slist<int> > ?
Should I put specific and structure-depending instructions in the constructors of the nodes classes?
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
To have separate constructors you will need to overload your constructor definition. You cannot have two constructions with the same prototype. One other reason to make your slist and dlist classes is that you can initialize prev=NULL in the constructor of the list class and not try and get your queue class to handle it.
Another way to do this would be to have a parameter in your queue class that tells you what kind of Queue you want to create, and you can set up your arguments based on that.
Another way to do this would be to have a parameter in your queue class that tells you what kind of Queue you want to create, and you can set up your arguments based on that.
Last edited by stilllearning; Oct 13th, 2008 at 7:53 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help me with my parsing tree
- Next Thread: Solving Towers of Hanoi
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





