Problem in "+" operator.in doubly circular link list.

M

Muzammil

i have problem with this operator "+" in doubly circular link list.(i
think i have problem with return type).
error is of instantiate error.(mean type dismatch) if any one can help
me please reply.


template <class T>
DoublyCircularLinkList<T> DoublyCircularLinkList<T>:: operator +
(const DoublyCircularLinkList& rhs) {
if (head==0 && rhs.head==0) {
return *this;
}
else if (rhs.head==0)
{

return *this;
}
else {

DoublyCircularLinkList said:
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;
}

}
 
M

Muzammil

template <class T>
DoublyCircularLinkList<T> DoublyCircularLinkList<T>:: operator +
(const DoublyCircularLinkList& rhs) {
if (head==0 && rhs.head==0) {
return *this;
}
else if (rhs.head==0)
{
return *this;
}
else {
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;
}
}
 
D

diamondback

template <class T>
DoublyCircularLinkList<T> DoublyCircularLinkList<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<T> pointer 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.
 
M

Muzammil

// This initial 'if' is unnecessary based on the current
implementation shown here




// You never instantiate this pointer...




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<T> pointer 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.

when i posted my questions most of time you all guys say why u not
use standard library functions.
reason i have to implement that particular function to understand what
the basics are not to use STL to make my work out....[mean i m student
of data structure course in undergraduate].
and thanks. for replying.
 
N

news.aioe.org

when i posted my questions most of time you all guys say why u not
use standard library functions.
reason i have to implement that particular function to understand what
the basics are not to use STL to make my work out....[mean i m student
of data structure course in undergraduate].
and thanks. for replying.

If "most of time", "all we guys" say why not use STL, then there is a
good reason for it, don't reinvent the wheel, there is already these
data structures and algorithms that STL provides which has been used
by engineers and thus considered efficient and bug free.

If your purpose is to learn, then why not mention it in your post?

Please also note that the person that responded had all good points
and did not respond with a one liner that just said "use STL".
 
M

Muzammil

when i posted my questions most of time  you all guys say why u not
use standard library functions.
reason i have to implement that particular function to understand what
the basics are not to use STL to make my work out....[mean i m student
of data structure course in undergraduate].
and thanks. for replying.

If "most of time", "all we guys" say why not use STL, then there is a
good reason for it, don't reinvent the wheel, there is already these
data structures and algorithms that STL provides which has been used
by engineers and thus considered efficient and bug free.

If your purpose is to learn, then why not mention it in your post?

Please also note that the person that responded had all good points
and did not respond with a one liner that just said "use STL".

ok.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top