Copy constructor for Queue ADT (as an Array)

This is a discussion on Copy constructor for Queue ADT (as an Array) within the C++ Programming forums, part of the General Programming Boards category; I have been struggling with the for two evenings, and can't seem to get anything other than garbage out. I ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Copy constructor for Queue ADT (as an Array)

    I have been struggling with the for two evenings, and can't seem to get anything other than garbage out. I know it is probably becasue of garbage in but I can't figure how to eliminate that.
    This is what I have for the copy constructor:
    Code:
    template<class Type>
    void queueType<Type>::copyQueue(const queueType<Type>& otherQueue)
    {
    	if (queueFront != NULL)
    		destroyQueue();
    	if (otherQueue.queueFront == NULL)
    	{
    		queueFront = 0;
    		queueRear = 0;
    		count = 0;
    	}
    	else
    	{
    		count = otherQueue.count;
    		maxQueueSize = otherQueue.maxQueueSize;
    		queueFront = otherQueue.queueFront;
    		queueRear = otherQueue.queueRear;
    		list = new Type[maxQueueSize];
    		assert (list != NULL);
    		for (int i = queueFront; i < queueRear; i++)
    			list[i] == otherQueue.list[i];
    	}
    }
    I am now getting the count correct for the "copy to" queue but that is because I made an explicit assignment. The elements in the "copy to" queue have garbage values.

    WHY?

    I understand that as element are deleted from the front of the queue and elements are added to the rear, the indices are advanced which is why I used queueFront and queueRear in the for loop, but that hasn't changed the garbage returned.

    What have I not included?

    Thanks for the help!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Problem is this line:

    Code:
    list[i] == otherQueue.list[i];

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Is it because I am assigning the address values instead of the element values?
    I need a second line to assign the element values?



    OH! I have a relational operator instead of an assignment operator!
    DUH!!!



    That was it I think, sort of! I got all the values assigned correctly, except the last, so I assigned the upper limit of my for loop to maxQueueSize and that fixed everything.

    I feel like such a duffus! I have been looking at that code for two evenings! Geeze OH Man!

    Thanks Brewbuck!
    Last edited by clegs; 11-28-2007 at 11:12 PM. Reason: Put 3 posts together

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 02:51 PM
  2. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 12:57 PM
  3. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 12:19 PM
  4. copy contents of array to single value?
    By mapunk in forum C Programming
    Replies: 3
    Last Post: 12-02-2005, 09:28 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21