Help with template queue

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Help with template queue

 
0
  #1
Oct 19th, 2008
I am getting a syntax error when I try to compile. After I write a little code I try to compile to catch any of my mistakes.
In this project I am to use a class queue template that is provided by my instructor here it is

  1. #ifndef TEMPLATEQ_H
  2.  
  3. #define TEMPLATEQ_H
  4.  
  5. #include <iostream>
  6. #include <new>
  7. #include <cstddef>
  8.  
  9. using namespace std;
  10.  
  11. class FullTemplateQ // Exception class
  12. {};
  13.  
  14.  
  15. class EmptyTemplateQ // Exception class
  16. {};
  17.  
  18.  
  19. template<class SomeType> // Node template class
  20. struct QueueNode
  21. {
  22. SomeType data; // Data stored in queue node
  23.  
  24. QueueNode<SomeType>* nextPtr; // Pointer to next queue node
  25. };
  26.  
  27.  
  28. template<class SomeType> // Circular queue template class
  29. class TemplateQ
  30. {
  31. private:
  32. QueueNode<SomeType>* rearPtr; // Pointer to rear of queue
  33.  
  34. QueueNode<SomeType>* frontPtr; // Pointer to front of queue
  35.  
  36. void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items
  37.  
  38. public:
  39. TemplateQ(); // Default constructor
  40.  
  41. ~TemplateQ(); // Destructor deallocates every node
  42.  
  43. void Enqueue(SomeType newData); // Adds newdata node to rear of queue
  44.  
  45. SomeType Dequeue(); // Removes data node from front of queue,
  46. // returning the stored data
  47.  
  48. bool IsFull() const; // Returns true if queue is full,
  49. // false otherwise
  50.  
  51. bool IsEmpty() const; // Returns true if queue is empty,
  52. // false otherwise
  53.  
  54. int Size() const; // Returns the number of items in queue
  55.  
  56. void ForwardPrint() const; // Prints queue, front to rear
  57.  
  58. void ReversePrint() const; // Prints queue, rear to front
  59. };
  60.  
  61. #include "templateq.cpp" // Very Important!! Do not delete!!
  62.  
  63. #endif

here is what I have so far

  1. #include <new>
  2. #include <cstddef>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. template <class someType>
  8. TemplateQ<someType>::TemplateQ()
  9. {
  10. rearPtr = NULL;
  11. frontPtr = NULL;
  12. }
When I try to compile this little bit of code I get this syntax error.

8 expected constructor, destructor, or type conversion before '<' token
8 expected `;' before '<'

What am I missing?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help with template queue

 
0
  #2
Oct 19th, 2008
It looks like you forgot to include your header file.

Try adding this to the top of your script--

  1.  
  2. #include "templateq.h"

Edit: Hmm interesting, I made the following changes and it managed to work--

  1.  
  2. //templateq.h
  3.  
  4. #ifndef TEMPLATEQ_H
  5.  
  6. #define TEMPLATEQ_H
  7.  
  8. #include <iostream>
  9. #include <new>
  10. #include <cstddef>
  11.  
  12. using namespace std;
  13.  
  14. class FullTemplateQ // Exception class
  15. {};
  16.  
  17.  
  18. class EmptyTemplateQ // Exception class
  19. {};
  20.  
  21.  
  22. template<class SomeType> // Node template class
  23. struct QueueNode
  24. {
  25. SomeType data; // Data stored in queue node
  26.  
  27. QueueNode<SomeType>* nextPtr; // Pointer to next queue node
  28. };
  29.  
  30.  
  31. template<class SomeType> // Circular queue template class
  32. class TemplateQ
  33. {
  34. private:
  35. QueueNode<SomeType>* rearPtr; // Pointer to rear of queue
  36.  
  37. QueueNode<SomeType>* frontPtr; // Pointer to front of queue
  38.  
  39. void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items
  40.  
  41. public:
  42. TemplateQ(); // Default constructor
  43.  
  44. ~TemplateQ(); // Destructor deallocates every node
  45.  
  46. void Enqueue(SomeType newData); // Adds newdata node to rear of queue
  47.  
  48. SomeType Dequeue(); // Removes data node from front of queue,
  49. // returning the stored data
  50.  
  51. bool IsFull() const; // Returns true if queue is full,
  52. // false otherwise
  53.  
  54. bool IsEmpty() const; // Returns true if queue is empty,
  55. // false otherwise
  56.  
  57. int Size() const; // Returns the number of items in queue
  58.  
  59. void ForwardPrint() const; // Prints queue, front to rear
  60.  
  61. void ReversePrint() const; // Prints queue, rear to front
  62. };
  63.  
  64. //#include "templateq.cpp" // Very Important!! Do not delete!!
  65.  
  66. #endif

Notice I commented out the #include for templateq.cpp.


  1. // templateq.cpp
  2.  
  3. #include "templateq.h"
  4. #include <new>
  5. #include <cstddef>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. template <class someType>
  11. TemplateQ<someType>::TemplateQ()
  12. {
  13. rearPtr = NULL;
  14. frontPtr = NULL;
  15. }
  16.  
  17. template<class someType>
  18. TemplateQ<someType>::~TemplateQ(){}

  1.  
  2. // driver program
  3.  
  4. #include "templateq.cpp"
  5.  
  6. int main(){
  7. TemplateQ<int> tq;
  8. return 0;
  9. }
Last edited by Alex Edwards; Oct 19th, 2008 at 1:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Re: Help with template queue

 
0
  #3
Oct 19th, 2008
according to my instructions #include "template.h" is not supposed to be there.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help with template queue

 
0
  #4
Oct 19th, 2008
Originally Posted by JustLearning View Post
according to my instructions #include "template.h" is not supposed to be there.
I'm not sure how you plan on defining the TemplateQ class without implementing the file with the prototype constructor, destructor and methods.

It's like trying to define something out of nothing.

The script you're working in has to have some knowledge of what it is defining.

The statement--

  1. template <class someType>
  2. TemplateQ<someType>::TemplateQ()
  3. {
  4. rearPtr = NULL;
  5. frontPtr = NULL;
  6. }

--is equivalent to saying I want to define the default constructor of the class TemplateQ<someType>, but how do you plan on doing that without giving the compiler a hint on the types declared in the class or what the class consists of?

You only include the headers new and cstddef, but not templateq.h

So let me ask you this. If you weren't instructed to add stdio or iostream or some other header that allows you to print to the Console but code is present that clearly shows that you should be implementing a header to allow you to do so, would you be reluctant to add that header?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Re: Help with template queue

 
0
  #5
Oct 19th, 2008
I am not allowed to modify the class file at all. This was provided by instructor with the instructions do not modify this file.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Re: Help with template queue

 
0
  #6
Oct 19th, 2008
Here is the instructions. These are what I am bounded to.

templateq.h
UNMODIFIED specification file for the TemplateQ class as supplied by the instructor
Note: This file must end with the preprocessor directive #include “templateq.cpp”
templateq.cpp (you must write this file)
This file will implement all member functions of the TemplateQ class.
Do NOT add #include “templateq.h” to this file!!!
main.cpp (you must write this file)
Contain your main function that serves as a test driver for the TemplateQ class.
This file must contain the preprocessor directive #include “templateq.h”

without these added instructions I would have put "template.h" in there.
Last edited by JustLearning; Oct 19th, 2008 at 2:17 pm. Reason: Highlight important notes
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Help with template queue

 
0
  #7
Oct 19th, 2008
I believe your Instructor is looking for you to do the following--

  1. // templateq.h
  2.  
  3. #ifndef TEMPLATEQ_H
  4.  
  5. #define TEMPLATEQ_H
  6.  
  7. #include <iostream>
  8. #include <new>
  9. #include <cstddef>
  10.  
  11. using namespace std;
  12.  
  13. class FullTemplateQ // Exception class
  14. {};
  15.  
  16.  
  17. class EmptyTemplateQ // Exception class
  18. {};
  19.  
  20.  
  21. template<class SomeType> // Node template class
  22. struct QueueNode
  23. {
  24. SomeType data; // Data stored in queue node
  25.  
  26. QueueNode<SomeType>* nextPtr; // Pointer to next queue node
  27. };
  28.  
  29.  
  30. template<class SomeType> // Circular queue template class
  31. class TemplateQ
  32. {
  33. private:
  34. QueueNode<SomeType>* rearPtr; // Pointer to rear of queue
  35.  
  36. QueueNode<SomeType>* frontPtr; // Pointer to front of queue
  37.  
  38. void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items
  39.  
  40. public:
  41. TemplateQ(); // Default constructor
  42.  
  43. ~TemplateQ(); // Destructor deallocates every node
  44.  
  45. void Enqueue(SomeType newData); // Adds newdata node to rear of queue
  46.  
  47. SomeType Dequeue(); // Removes data node from front of queue,
  48. // returning the stored data
  49.  
  50. bool IsFull() const; // Returns true if queue is full,
  51. // false otherwise
  52.  
  53. bool IsEmpty() const; // Returns true if queue is empty,
  54. // false otherwise
  55.  
  56. int Size() const; // Returns the number of items in queue
  57.  
  58. void ForwardPrint() const; // Prints queue, front to rear
  59.  
  60. void ReversePrint() const; // Prints queue, rear to front
  61. };
  62.  
  63. #include "templateq.cpp" // Very Important!! Do not delete!!
  64.  
  65. #endif

  1.  
  2. //templateq.cpp
  3.  
  4. #ifdef TEMPLATEQ_H
  5.  
  6. TEMPLATEQ_H
  7.  
  8. #include <iostream>
  9.  
  10.  
  11. template <class someType>
  12. TemplateQ<someType>::TemplateQ()
  13. {
  14. rearPtr = NULL;
  15. frontPtr = NULL;
  16. cout << "Instantiation successful!" << endl;
  17. }
  18.  
  19. template <class someType>
  20. TemplateQ<someType>::~TemplateQ(){
  21. cout << "Destruction successful!" << endl;
  22. }
  23.  
  24. #endif

  1.  
  2. // driver program
  3.  
  4. #include "templateq.h"
  5. #include <iostream>
  6.  
  7. using std::cin;
  8. using std::cout;
  9. using std::endl;
  10.  
  11.  
  12. int main(){
  13. TemplateQ<int> tq;
  14. cin.ignore(INT_MAX, '\n');
  15. cin.get();
  16. return 0;
  17. }
Last edited by Alex Edwards; Oct 19th, 2008 at 5:46 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC