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

Discussion in 'C++' started by Muzammil, Sep 23, 2008.

  1. Muzammil

    Muzammil Guest

    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;
    }

    }
    Muzammil, Sep 23, 2008
    #1
    1. Advertising

  2. Muzammil

    Muzammil Guest

    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;
    }
    }
    Muzammil, Sep 23, 2008
    #2
    1. Advertising

  3. Muzammil

    diamondback Guest

    On Sep 23, 8:04 am, Muzammil <> 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.
    diamondback, Sep 23, 2008
    #3
  4. Muzammil

    Muzammil Guest

    On Sep 23, 9:08 am, diamondback <> wrote:
    > On Sep 23, 8:04 am, Muzammil <> 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.
    Muzammil, Sep 23, 2008
    #4
  5. >
    > 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".
    news.aioe.org, Sep 23, 2008
    #5
  6. Muzammil

    Muzammil Guest

    On Sep 23, 2:11 pm, "news.aioe.org" <> 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.
    Muzammil, Sep 24, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    728
    Jonathan Mcdougall
    Jan 3, 2005
  2. sudhirlko2001

    Doubly Link List

    sudhirlko2001, Sep 2, 2005, in forum: C Programming
    Replies:
    8
    Views:
    375
    sudhirlko2001
    Sep 26, 2005
  3. cyclic doubly link list

    , Oct 17, 2005, in forum: C Programming
    Replies:
    5
    Views:
    405
    Chris Dollin
    Oct 18, 2005
  4. Replies:
    3
    Views:
    350
    osmium
    May 18, 2007
  5. ravi

    Sorting a doubly link list

    ravi, Jul 21, 2007, in forum: C Programming
    Replies:
    3
    Views:
    371
    Ben Pfaff
    Jul 21, 2007
Loading...

Share This Page