Template specification - Constructors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Template specification - Constructors

 
-1
  #1
Oct 13th, 2008
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:

  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using std::cout;
  5. using std::endl;
  6. using std::cin;
  7.  
  8. template <class D>
  9. struct slist {
  10. public:
  11. D data;
  12. slist<D> *next;
  13. };
  14.  
  15. template <class D>
  16. struct dlist {
  17. public:
  18. D data;
  19. dlist<D> *next;
  20. dlist<D> *prev;
  21. };
  22.  
  23. template <class S>
  24. class queue {
  25. S *start;
  26. string name;
  27. public:
  28. queue(void);
  29. ~queue(void);
  30. };
  31.  
  32. template <class D>
  33. queue<slist<D> >::queue(void) {
  34. start = new slist<D>;
  35. cout << endl << "Constructor slist Test" << endl;
  36. return;
  37. }
  38.  
  39. template <class D>
  40. queue<dlist<D> >::queue(void) {
  41. start = new dlist<D>:
  42. cout << endl << "Constructor dlist Test" << endl;
  43. return;
  44. }
  45.  
  46. template <class S>
  47. queue<S>::~queue(void) {
  48. delete start;
  49. cout << endl << "Destructor Test" << endl;
  50. return;
  51. }
  52.  
  53. int main(void) {
  54. queue<slist<int> > ob1;
  55. cin.get();
  56. return EXIT_SUCCESS;
  57. }

How may I differentiate the constructors?
This code isn't compiling for the following reason, that I don't understand.

  1. error: invalid use of incomplete type ‘class queue<slist<D> >’
  2. error: declaration of ‘class queue<slist<D> >’
  3. error: invalid use of incomplete type ‘class queue<dlist<D> >’
  4. 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 ^^"
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Template specification - Constructors

 
0
  #2
Oct 13th, 2008
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.

  1. template <class T>
  2. struct slist {
  3. public:
  4. T Data;
  5. slist<T> *next;
  6. };
  7.  
  8. template <class T>
  9. struct Dlist {
  10. public:
  11. T Data;
  12. Dlist<T> *next;
  13. Dlist<T> *prev;
  14. };
  15.  
  16. template <class T>
  17. class myQueue{
  18. public:
  19. myQueue<T>();
  20. ~myQueue<T>(void);
  21. private:
  22. T *start;
  23. };
  24.  
  25. template <class T>
  26. myQueue<T>::myQueue() {
  27. start = new T;
  28. return;
  29. }
  30.  
  31. template <class T>
  32. myQueue<T>::~myQueue(void) {
  33. delete start;
  34. return;
  35. }
  36.  
  37.  
  38. int main() {
  39. myQueue<struct slist<int>> sobj;
  40. myQueue<struct Dlist<int>> dobj;
  41.  
  42. cin.get();
  43. return EXIT_SUCCESS;
  44. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Template specification - Constructors

 
0
  #3
Oct 13th, 2008
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

  1. template <class T>
  2. myQueue<T>::myQueue() {
  3. start = new T;
  4. return;
  5. }

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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Template specification - Constructors

 
1
  #4
Oct 13th, 2008
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.
Last edited by stilllearning; Oct 13th, 2008 at 7:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Template specification - Constructors

 
0
  #5
Oct 13th, 2008
I understand. I was playing around with solutions involving templates (since I wanted to learn them!) but it seems like the eventual parameter "bool DoubleLinked = true" is the best (and easier for sure) solution.

Thanks for the advices!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC