| | |
Help with template queue
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
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
here is what I have so far
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?
In this project I am to use a class queue template that is provided by my instructor here it is
C++ Syntax (Toggle Plain Text)
#ifndef TEMPLATEQ_H #define TEMPLATEQ_H #include <iostream> #include <new> #include <cstddef> using namespace std; class FullTemplateQ // Exception class {}; class EmptyTemplateQ // Exception class {}; template<class SomeType> // Node template class struct QueueNode { SomeType data; // Data stored in queue node QueueNode<SomeType>* nextPtr; // Pointer to next queue node }; template<class SomeType> // Circular queue template class class TemplateQ { private: QueueNode<SomeType>* rearPtr; // Pointer to rear of queue QueueNode<SomeType>* frontPtr; // Pointer to front of queue void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items public: TemplateQ(); // Default constructor ~TemplateQ(); // Destructor deallocates every node void Enqueue(SomeType newData); // Adds newdata node to rear of queue SomeType Dequeue(); // Removes data node from front of queue, // returning the stored data bool IsFull() const; // Returns true if queue is full, // false otherwise bool IsEmpty() const; // Returns true if queue is empty, // false otherwise int Size() const; // Returns the number of items in queue void ForwardPrint() const; // Prints queue, front to rear void ReversePrint() const; // Prints queue, rear to front }; #include "templateq.cpp" // Very Important!! Do not delete!! #endif
here is what I have so far
C++ Syntax (Toggle Plain Text)
#include <new> #include <cstddef> using namespace std; template <class someType> TemplateQ<someType>::TemplateQ() { rearPtr = NULL; frontPtr = NULL; }
8 expected constructor, destructor, or type conversion before '<' token
8 expected `;' before '<'
What am I missing?
It looks like you forgot to include your header file.
Try adding this to the top of your script--
Edit: Hmm interesting, I made the following changes and it managed to work--
Notice I commented out the #include for templateq.cpp.
Try adding this to the top of your script--
c++ Syntax (Toggle Plain Text)
#include "templateq.h"
Edit: Hmm interesting, I made the following changes and it managed to work--
c++ Syntax (Toggle Plain Text)
//templateq.h #ifndef TEMPLATEQ_H #define TEMPLATEQ_H #include <iostream> #include <new> #include <cstddef> using namespace std; class FullTemplateQ // Exception class {}; class EmptyTemplateQ // Exception class {}; template<class SomeType> // Node template class struct QueueNode { SomeType data; // Data stored in queue node QueueNode<SomeType>* nextPtr; // Pointer to next queue node }; template<class SomeType> // Circular queue template class class TemplateQ { private: QueueNode<SomeType>* rearPtr; // Pointer to rear of queue QueueNode<SomeType>* frontPtr; // Pointer to front of queue void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items public: TemplateQ(); // Default constructor ~TemplateQ(); // Destructor deallocates every node void Enqueue(SomeType newData); // Adds newdata node to rear of queue SomeType Dequeue(); // Removes data node from front of queue, // returning the stored data bool IsFull() const; // Returns true if queue is full, // false otherwise bool IsEmpty() const; // Returns true if queue is empty, // false otherwise int Size() const; // Returns the number of items in queue void ForwardPrint() const; // Prints queue, front to rear void ReversePrint() const; // Prints queue, rear to front }; //#include "templateq.cpp" // Very Important!! Do not delete!! #endif
Notice I commented out the #include for templateq.cpp.
c++ Syntax (Toggle Plain Text)
// templateq.cpp #include "templateq.h" #include <new> #include <cstddef> using namespace std; template <class someType> TemplateQ<someType>::TemplateQ() { rearPtr = NULL; frontPtr = NULL; } template<class someType> TemplateQ<someType>::~TemplateQ(){}
c++ Syntax (Toggle Plain Text)
// driver program #include "templateq.cpp" int main(){ TemplateQ<int> tq; return 0; }
Last edited by Alex Edwards; Oct 19th, 2008 at 1:55 pm.
•
•
•
•
according to my instructions #include "template.h" is not supposed to be there.
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--
c++ Syntax (Toggle Plain Text)
template <class someType> TemplateQ<someType>::TemplateQ() { rearPtr = NULL; frontPtr = NULL; }
--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?
•
•
Join Date: Sep 2008
Posts: 31
Reputation:
Solved Threads: 0
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.
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
I believe your Instructor is looking for you to do the following--
c++ Syntax (Toggle Plain Text)
// templateq.h #ifndef TEMPLATEQ_H #define TEMPLATEQ_H #include <iostream> #include <new> #include <cstddef> using namespace std; class FullTemplateQ // Exception class {}; class EmptyTemplateQ // Exception class {}; template<class SomeType> // Node template class struct QueueNode { SomeType data; // Data stored in queue node QueueNode<SomeType>* nextPtr; // Pointer to next queue node }; template<class SomeType> // Circular queue template class class TemplateQ { private: QueueNode<SomeType>* rearPtr; // Pointer to rear of queue QueueNode<SomeType>* frontPtr; // Pointer to front of queue void PrintNext(QueueNode<SomeType>* tempPtr) const; // Print trailing items public: TemplateQ(); // Default constructor ~TemplateQ(); // Destructor deallocates every node void Enqueue(SomeType newData); // Adds newdata node to rear of queue SomeType Dequeue(); // Removes data node from front of queue, // returning the stored data bool IsFull() const; // Returns true if queue is full, // false otherwise bool IsEmpty() const; // Returns true if queue is empty, // false otherwise int Size() const; // Returns the number of items in queue void ForwardPrint() const; // Prints queue, front to rear void ReversePrint() const; // Prints queue, rear to front }; #include "templateq.cpp" // Very Important!! Do not delete!! #endif
c++ Syntax (Toggle Plain Text)
//templateq.cpp #ifdef TEMPLATEQ_H TEMPLATEQ_H #include <iostream> template <class someType> TemplateQ<someType>::TemplateQ() { rearPtr = NULL; frontPtr = NULL; cout << "Instantiation successful!" << endl; } template <class someType> TemplateQ<someType>::~TemplateQ(){ cout << "Destruction successful!" << endl; } #endif
c++ Syntax (Toggle Plain Text)
// driver program #include "templateq.h" #include <iostream> using std::cin; using std::cout; using std::endl; int main(){ TemplateQ<int> tq; cin.ignore(INT_MAX, '\n'); cin.get(); return 0; }
Last edited by Alex Edwards; Oct 19th, 2008 at 5:46 pm.
![]() |
Similar Threads
- Help on stack , queue, palindrome program... (C++)
- How to solve ( Sort a queue )? (C++)
- printing elements in a queue (C++)
- radix sort using queue and linked list (C++)
- can ANYONE help? need to make a queue (C)
- Stack Queue Fstream (C++)
- help with queue plz (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: CLR Console application vs Win32 Console application
- Next Thread: Question about Inheritance/Classes
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





