Greetings all DaniWeb people, long time no see.
I have a (hopefully) easy question. Let's jump into the code.
My test harness:
cout << "--------------------------------------------------------" << endl;
cout << "Begin Copy Constructor Testing" << endl;
cout << "--------------------------------------------------------" << endl << endl;
dlSL = new DLSortedList();
for (int i = 0; i <= 30; i += 2) dlSL->insert(i);
dlSL->insert(11);
cout << "Original list before copy:" << endl;
dlSL->display();
//clone the original list
DLSortedList * newList = new DLSortedList(dlSL&);
My constructor for DLSortedList:
DLSortedList::DLSortedList() :
head(new DLNode::DLNode()),
tail(new DLNode::DLNode(head, 0, NULL)),
numItems(0),
cursor(head),
cursPosition(0) {
head->item = INT_MIN;
tail->item = INT_MAX;
head->next = tail;
}
Copy constructor:
DLSortedList::DLSortedList(const DLSortedList& source) :
head(new DLNode::DLNode()),
tail(new DLNode::DLNode(head, 0, NULL)),
cursor(head),
cursPosition(0),
numItems(0) {
head->item = INT_MIN;
tail->item = INT_MAX;
//is the source list empty?
//if so, just point head at tail
if (source.isEmpty())
head->next = tail;
//otherwise, we have to iterate through the old list and copy each node
else {
//setup first node
DLNode * toInsert = new DLNode();
toInsert->previous = head;
toInsert->next = tail;
tail->previous = toInsert;
head->next = toInsert;
toInsert->item = source.head->next->item;
numItems += 1;
cursPosition += 1;
cursor = toInsert;
//now copy the rest of the source
DLNode * newPtr = toInsert;
//newPtr points to last node in new list
//sourcePtr points to nodes in original list
//as long as the sourcePtr isn't pointing at the tail
//we create a new DLNode, adjust pointers in the copy, and copy the item
for (DLNode *sourcePtr = source.head->next->next;
sourcePtr != tail;
sourcePtr = sourcePtr->next) {
newPtr->next = new DLNode();
newPtr = newPtr->next;
newPtr->item = sourcePtr->item;
newPtr->next = tail;
newPtr->previous = tail->previous;
tail->previous = newPtr;
numItems += 1;
//should we move the cursor to the new item?
if (cursPosition < source.cursPosition) {
cursPosition += 1;
cursor = newPtr;
} //end if
} //end for
} //end else
} //end copy constructor
The class compiles, but the test will not.
I have tried to invoke the copy constructor a number of ways, but it is just not working.
DLSortedList * newList = new DLSortedList(dlSL&);
//generates error: expected primary-expression before ‘)’ token
DLSortedList newList(dlSL&);
//generates error: expected primary-expression before ‘)’ token
I tried to invoke the copy constructor a few other ways but the errors generated there seemed to be worse. This is close, just need a bit of help to get over my hump.
Thanks all,
Vance