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

#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

#include <new>
#include <cstddef>

using namespace std;


template <class someType>
TemplateQ<someType>::TemplateQ()
{
    rearPtr = NULL;
    frontPtr = NULL;
}

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?

Recommended Answers

All 6 Replies

It looks like you forgot to include your header file.

Try adding this to the top of your script--

#include "templateq.h"

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

//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.

// 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(){}
// driver program

#include "templateq.cpp"

int main(){
    TemplateQ<int> tq;
    return 0;
}

according to my instructions #include "template.h" is not supposed to be there.

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--

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?

I am not allowed to modify the class file at all. This was provided by instructor with the instructions do not modify this file.

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.

I believe your Instructor is looking for you to do the following--

// 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
//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
// 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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.