Pushing onto a Queue

Having a little trouble pushing a new entry onto a queue. (note this is a double-ended queue, so I can push/pop from either end).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    //instantiate a new queue
    Deque<D_Node<size_t> > q;

    //create a new D_Node to push onto the Deque
    D_Node<int> first_node(1, NULL, NULL);
    D_Node<int> *head_ptr;
    D_Node<int> *tail_ptr;
    head_ptr = &first_node;
    tail_ptr = &first_node;

    //push the D_Node onto the front of the Deque
    q.push_front(first_node);

   return 0;
}


1
2
3
4
5
template <class Item>
void Deque<Item>::push_front( const Item &entry )
{
   my_front = entry;
}


The compiler is telling me that there is no matching function call, but I don't see why? Anyone see this?

Thanks!

i think you dont define operator = for item of course in in your exm it be D_Node where my_front = entry; ! but you must copy all of your code
Last edited on
Thank you, but I don't understand this response. Can anybody take a look at this for me? Thanks again.
The compiler is telling me that there is no matching function call, but I don't see why?

Perhaps it's because you're passing in a D_Node<int> rather than a D_Node<size_t>. You should change all the D_Node<int>s accordingly in main. This should make it compile fine.

You'll be out of luck for it running fine, however. This:

my_front = entry;

merely overwrites the front entry. That will only work if the queue is empty. If you keep calling push_front it will keep overwriting the front and thus it will not be adding anything to the queue. Instead, if the queue is not empty, you need to keep track of your (old) my_front, set my_front to the new entry, and set the new entry's next to the (old) my_front.

ahura24 wrote:
i think you dont define operator =

This will be a problem if your D_Node class uses dynamic memory.
Last edited on
By the way, my_front is a member variable in Deque set up as a pointer like this:

my_namespace::D_Node<Item> *my_front;

so I tried changing my_front = entry; to my_front = &entry;, although I don't think that is correct. It wasn't.

My error isn't "no matching function call", but rather "cannot convert '...D_Node<unsigned int>' to '...D_Node<unsigned int> >*' in assignment.

I'm stuck.

Last edited on
Perhaps it's because you're passing in a D_Node<int> rather than a D_Node<size_t>. You should change all the D_Node<int>s accordingly in main. This should make it compile fine.


I noticed that, too, and changed everything to size_t before my last post. I should have mentioned that.

Thanks for the warning on my_front = entry. Since my_front is a pointer to the front entry, I was thinking that this would just change which entry my_front is pointing to, no?

So, now I have two questions:

1. How do handle the compilation error about cannot convert...
2. How to get the the my_front pointer to point to the correct entry. This one doesn't sound that tough.

It's question 1 that I need to solve first since the thing won't compile right now.

Thanks!
Thanks everybody. I think that I got this solved by taking a different approach.

I appreciate the help!
yes , you are right .
i just look at

template <class Item>
void Deque<Item>::push_front( const Item &entry )
{
my_front = entry;
}

in this code if operator = not define the compiler take error . but if i dont know about all of your code and i cant give my idea.
Last edited on
Topic archived. No new replies allowed.