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 " " :P
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:

#include <iostream>
#include <string.h>

using std::cout;
using std::endl;
using std::cin;

template <class D>
struct slist {
	public:
	D data;
	slist<D> *next;
};

template <class D>
struct dlist {
	public:
	D data;
	dlist<D> *next;
	dlist<D> *prev;
};

template <class S>
class queue {
	S *start;
	string name;
	public:
	queue(void);
	~queue(void);
};

template <class D>
queue<slist<D> >::queue(void) {
	start = new slist<D>;
	cout << endl << "Constructor slist Test" << endl;
	return;
}

template <class D>
queue<dlist<D> >::queue(void) {
	start = new dlist<D>:
	cout << endl << "Constructor dlist Test" << endl;
	return;
}

template <class S>
queue<S>::~queue(void) {
	delete start;
	cout << endl << "Destructor Test" << endl;
	return;
}

int main(void) {
	queue<slist<int> > ob1;
	cin.get();
	return EXIT_SUCCESS;
}

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

error: invalid use of incomplete type ‘class queue<slist<D> >’
error: declaration of ‘class queue<slist<D> >’
error: invalid use of incomplete type ‘class queue<dlist<D> >’
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 ^^"

Recommended Answers

All 4 Replies

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.

template <class T>
struct slist {
	public:
	T Data;
	slist<T> *next;
};
 
template <class T>
struct Dlist {
	public:
	T Data;
	Dlist<T> *next;
	Dlist<T> *prev;
};
 
template <class T>
class myQueue{
	public:
	myQueue<T>();
	~myQueue<T>(void);
private:
  T *start;
};
 
template <class T>
myQueue<T>::myQueue() {
  start = new T;
	return;
}
 
template <class T> 
myQueue<T>::~myQueue(void) {
	delete start; 
	return;
}

 
int main() {
  myQueue<struct slist<int>> sobj;
  myQueue<struct Dlist<int>> dobj;

	cin.get();
	return EXIT_SUCCESS;
}

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

template <class T>
myQueue<T>::myQueue() {
start = new T;
return;
}

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?

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.

commented: It helped! Thanks :) +1

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! :)

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.