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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Muzammil

    #1

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

    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>
    DoublyCircularL inkList<TDoubly CircularLinkLis t<T>:: operator +
    (const DoublyCircularL inkList& rhs) {
    if (head==0 && rhs.head==0) {
    return *this;
    }
    else if (rhs.head==0)
    {

    return *this;
    }
    else {

    DoublyCircularL inkList<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

    #2
    Re: Problem in &quot;+&quot ; operator.in doubly circular link list.

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

    Comment

    • diamondback

      #3
      Re: Problem in &quot;+&quot ; operator.in doubly circular link list.

      On Sep 23, 8:04 am, Muzammil <muzammilPeer.. .@gmail.comwrot e:
      template <class T>
      DoublyCircularL inkList<TDoubly CircularLinkLis t<T>:: operator +
      (const  DoublyCircularL inkList& 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...
      DoublyCircularL inkList<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 DoublyCircularL inkList<Tpointe r with this
      line:
      DoublyCircularL inkList<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:
      DoublyCircularL inkList<T>* Return = new DoublyCircularL inkList<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 >
      DoublyCircularL inkList<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.

      Comment

      • Muzammil

        #4
        Re: Problem in &quot;+&quot ; operator.in doubly circular link list.

        On Sep 23, 9:08 am, diamondback <christopher... .@gmail.comwrot e:
        On Sep 23, 8:04 am, Muzammil <muzammilPeer.. .@gmail.comwrot e:
        >
        template <class T>
        DoublyCircularL inkList<TDoubly CircularLinkLis t<T>:: operator +
        (const  DoublyCircularL inkList& 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...
        >
        >
        >
        DoublyCircularL inkList<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 DoublyCircularL inkList<Tpointe r with this
        line:
        DoublyCircularL inkList<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:
        DoublyCircularL inkList<T>* Return = new DoublyCircularL inkList<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 >
        DoublyCircularL inkList<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.

        Comment

        • news.aioe.org

          #5
          Re: Problem in &quot;+&quot ; operator.in doubly circular link list.

          >
          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".

          Comment

          • Muzammil

            #6
            Re: Problem in &quot;+&quot ; operator.in doubly circular link list.

            On Sep 23, 2:11 pm, "news.aioe. org" <bigmur...@yaho o.comwrote:
            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.

            Comment

            Working...