Constructor Question

I'm working on an assignment involving double-end queues (a "deque") within a template class and am a bit confused about creating the constructor. Each deque object is supposed to be of a type "D_Node", which is a class that we just finished writing (doubly-linked list nodes).

The Deque class has three member variables:
D_Node<Item> *my_front
D_Node<Item> *my_back
size_t my_size

The constructor looks like this:
1
2
3
4
5
6
7
template <class Item>
Deque<Item>::Deque( const Deque &source )
{
   my_front = NULL;
   my_rear = NULL;
   my_size = source;
}


I'm not having much success declaring a new Deque. I've tried a few things, none of which compile. Among them:

Deque<size_t> q(10); //no significance to the 10, just a placeholder

size_t size = 10;
Deque<size_t> q(size);

I think, though, that the Deque has to consist of D_Nodes, right? So, wouldn't I do something like:

Deque<D_Node> q(something); ???

I'm not seeing the right path to get this started. I appreciate any help.

Thanks.
Last edited on
I made some progress on this, but still can't quite get there. Correct me if I'm wrong, but I think that my constructor needs to look like this (using the default constructor):

Deque<D_Node<int> > q1;

Assuming this is correct, I still can't figure out how to call the overloaded constructor. I tried this:

1
2
D_Node first_node(10, NULL, NULL); //consistent with the constructor for the D_Node class
Deque<D_Node<int> > q2(first_node);


but the compiler still doesn't like me. Any suggestions? Thanks!
That constructor should be taking a size_t as a parameter, not another Deque.
I thought that might not be right, but I was given the constructor prototype, so I was reluctant to change it.

I made that change, but it still doesn't compile. I get an error that says
cannot convert ...::D_Node<unsigned int> to size_t in assignment
. My constructor in the D_Node template class looks like this:

1
2
3
4
5
6
7
8
	template <class Item>
	D_Node<Item>::D_Node( const Item& init_data,
		   D_Node* init_fore, D_Node* init_back )
	{
		data_field = init_data;
		link_fore = init_fore;
		link_back = init_back;
	}


where the member variable data_field is type Item.

I can't find anything that explains how to do this constructor. Do you see what I am doing wrong?

Thanks again.
Last edited on
closed account (D80DSL3A)
Good point but an empty list is being created so my_size = 0 and the constructor requires no argument.
The list is clearly empty since my_front = my_rear = NULL.
That's a good point, too. my_size is supposed to set the maximum size of the queue. Can you see a way that I can call the overloaded constructor? I can't figure out how to get in there. Thanks.
closed account (D80DSL3A)
Maybe it needs to be my_size = source.my_size;? This seems weird though since the list size is zero until some D_Nodes are actually allocated to it.

Is the new list supposed to be a copy of source?

About the D_Node constructor. I have one that is similar except it also links the D_Nodes passed in the call to the new D_Node. It seems to work great!
1
2
3
4
5
6
7
8
9
10
11
12
template <class Item>
D_Node<Item>::D_Node( const Item& init_data,
	   D_Node* init_fore, D_Node* init_back )
{
	data_field = init_data;
	link_fore = init_fore;
	link_back = init_back;

        // link the given D_Nodes to the new one.
        if( init_fore ) init_fore->link_back = this;
        if( init_back ) init_back->link_fore = this;
} 

I think that you're right. As I think more about it, my_size isn't the maximum size of the queue (I was thinking of a circular queue), but rather the number of entries in it at any given moment. So, when it is constructed, there are zero items, by definition.

Thanks for the help. I'll just use the default constructor for now.
closed account (D80DSL3A)
Sorry I didn't see it but the constructor prototype you were given is surely meant for creation of a copy constructor. This way your Deque class has a copy constructor.

You would want to allocate all of the D_Nodes for the new Deque within this constructor and copy the data_fields from the source Deque to the new Deque.
Topic archived. No new replies allowed.