On Sep 23, 8:04*am, Muzammil <muzammilPeer...@gmail.comwrote:
template <class T>
DoublyCircularLinkList<TDoublyCircularLinkList<T>: : operator +
(const *DoublyCircularLinkList& rhs) {
// This initial 'if' is unnecessary based on the current
implementation shown here
if (head==0 && rhs.head==0) {
return *this;}
else if (rhs.head==0)
{
return *this;}
else {
// You never instantiate this pointer...
DoublyCircularLinkList<T>* Return;
Return->Insert(5);
DNode<T>* TEMP=head;
do
{
Return->Insert(TEMP->Get());
TEMP=TEMP->GetNextPtr();} while (TEMP!=tail->GetNextPtr());
TEMP=rhs.head;
do
{
Return->Insert(TEMP->Get());
TEMP=TEMP->GetNextPtr();
} while (TEMP!=rhs.tail->GetNextPtr());
return *Return;
}
}
First, you do not need 'if (head==0 && rhs.head==0)' and 'else if
(rhs.head==0)'. The behavior of both the conditions is the same and
the fact that 'head==0' does not change the action taken so all you
really need is the 'else if' case.
Second, you declare a DoublyCircularLinkList<Tpointer with this
line:
DoublyCircularLinkList<T>* Return;
However, you never actually instantiate the object before you try to
access it. That is, 'Return' is merely a pointer to memory that does
not "exist". You must first instantiate the memory something like
this:
DoublyCircularLinkList<T>* Return = new DoublyCircularLinkList<T>();
Also, bear in mind a few other things
1) You are returning by value, which means you will return a copy of
the object you have created and manipulated. That will reflect back on
the semantics of your copy constructor ( template< typename T >
DoublyCircularLinkList<T>() ). Be sure the copy constructor does what
you want it to do.
2) For the sake of clarity, think very seriously about NOT overloading
operator+ to concatenate. In general, operator+ means 'add' when
applied to primitive data types. Addition is the behavior that most
people expect. While it is true that various complex data types do
extend the meaning of operator+, your intent may not be obvious,
especially with a template class. Also, correct implementation is non-
trivial. Read "C++ Coding Standards" (Sutter, Alexandrescu), Item 26
for more discussion.
3) Why are you making this list in the first place? Unless you are
absolutely sure that you need it, why not try one of the standard
containers instead (vector, list, deque, etc)?
I hope that helps.