Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in "+" operator.in doubly circular link list.

Reply
Thread Tools

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

 
 
Muzammil
Guest
Posts: n/a
 
      09-23-2008
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<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;
}

}
 
Reply With Quote
 
 
 
 
Muzammil
Guest
Posts: n/a
 
      09-23-2008
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;
}
}
 
Reply With Quote
 
 
 
 
diamondback
Guest
Posts: n/a
 
      09-23-2008
On Sep 23, 8:04*am, Muzammil <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Muzammil
Guest
Posts: n/a
 
      09-23-2008
On Sep 23, 9:08*am, diamondback <(E-Mail Removed)> wrote:
> On Sep 23, 8:04*am, Muzammil <(E-Mail Removed)> wrote:
>
> > 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.


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.
 
Reply With Quote
 
news.aioe.org
Guest
Posts: n/a
 
      09-23-2008
>
> 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".
 
Reply With Quote
 
Muzammil
Guest
Posts: n/a
 
      09-24-2008
On Sep 23, 2:11*pm, "news.aioe.org" <(E-Mail Removed)> wrote:
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a doubly link list ravi C Programming 3 07-21-2007 08:24 AM
doubly link list formation maruf.syfullah@gmail.com C++ 3 05-18-2007 06:58 PM
cyclic doubly link list free2cric@yahoo.com C Programming 5 10-18-2005 08:11 AM
Doubly Link List sudhirlko2001 C Programming 8 09-26-2005 01:44 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM



Advertisments