943,526 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2191
  • C++ RSS
Oct 13th, 2008
-1

Template specification - Constructors

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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 ^^"
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Oct 13th, 2008
0

Re: Template specification - Constructors

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)
  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. }
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 13th, 2008
0

Re: Template specification - Constructors

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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Oct 13th, 2008
1

Re: Template specification - Constructors

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 13th, 2008
0

Re: Template specification - Constructors

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!
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help me with my parsing tree
Next Thread in C++ Forum Timeline: Solving Towers of Hanoi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC