简体   繁体   中英

Calling copyQueue in main.cpp

I am thoroughly confused as to what my instructor is asking me to do. The sample output of our program that he provided say that : "Sending q1 as an argument to test the copy constructor"

So I am unsure what he is asking.

I have created a copyQueue Function here:

template <class Type>
void queueType<Type>::copyQueue(/*const*/ queueType<Type>& otherQueue)
//I omitted the const qualifier as I kept getting error messages when it was included
{
    if(otherQueue.queueFront=NULL) {
        initializeQueue();
    } 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];
    }//end else
}//end function

And a Function that will print the contents of the queue:

template <class Type>
void queueType<Type>::debugArray() {
    for(int current =queueFront; current<count; current++) {
        cout<<"[" << current<<"] ,"<< list[current]<<" ";
    }
    cout<<"\n"<<endl;
} //end debugQueue

I am I to assume that I should call copyQueue like such in the main.cpp:

#include <iostream>
#include "queueAsArray.h"

using namespace std;

int main() {
    queueType<int> q1;
    queueType<int> q2;

    for(int i= 0; i<10; i++)
    q1.addQueue(i);
    q1.debugQueue();

    q1.copyQueue(q1);
    q1.debugQueue();

    return 0;
}

When I do this nothing happens the second time I call debugQueue . I have sample output and from it I am to assume that I need to send q1 as an argument to the copyQueue function and then call debugQueue again to show that the queue still has components in the queue.

I am a little lost and confused as to why it will not print a second time. Any thoughts? This is just a snippet of my work so If you need the entire implementation file or the complete main.cpp file let me know. Or if you need the sample output example I can also provide that too.

Thanks

I think that what your instructor wants you to do is to test the copy constructor. In your main you should have:

int main() {
    queueType<int> q1;
    queueType<int> q2;

    for(int i= 0; i<10; i++)
        q1.addQueue(i);
    q1.debugQueue();

    q2.copyQueue(q1); // q2 here
    q2.debugQueue();  // q2 here

    return 0;
}

Then the two debugQueue calls should print the same array, which proves that queue q1 was correctly copied into queue q2 .

You also have an error on your code, inside the copyQueue function:

if(otherQueue.queueFront = NULL)

should be

if(otherQueue.queueFront == NULL)

with double equality signs. A simple = just erases otherQueue (and this is probably why your compiler complained about the const ). The double == tests for equality. If you correct this you can add the const back (it's a very bad idea to have a copy constructor with a non-const parameter).

The copy constructor is a very specific construct, not just "copying objects" as the other answer seemed to suggest. Read this article for a quick primer on it, and just google "copy constructor" for a few more.

You may also want to read this snippet about why if you have a copy constructor, you probably need an assignment operator and a virtual destructor.

You should also read this entire section about const correctness to diagnose your const error as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM