Linked-List help

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

    Linked-List help

    <snippet>

    class Node1
    {
    Node2* link;

    public:
    void insertNode2(Nod e2* innerNode)
    {
    link->getNext() = innerNode; //***error -- non-lvalue in
    assignment
    }
    };

    class Node2
    {
    Node2* next;

    public:
    Node2* getNext(){ return next;}
    };

    ---

    Why Am I getting an error denoted at ***?


    wewewewe



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.510 / Virus Database: 307 - Release Date: 15/08/2003


  • White Wolf

    #2
    Re: Linked-List help

    Ying Yang wrote:[color=blue]
    > <snippet>
    >
    > class Node1
    > {
    > Node2* link;
    >
    > public:
    > void insertNode2(Nod e2* innerNode)
    > {
    > link->getNext() = innerNode; //***error -- non-lvalue in
    > assignment
    > }
    > };
    >
    > class Node2
    > {
    > Node2* next;
    >
    > public:
    > Node2* getNext(){ return next;}
    > };
    >
    > ---
    >
    > Why Am I getting an error denoted at ***?[/color]

    Because you try to assign a new value to an unnamed temporary variable.

    --
    WW aka Attila


    Comment

    • White Wolf

      #3
      Re: Linked-List help

      White Wolf wrote:
      [SNIP][color=blue][color=green]
      >> link->getNext() = innerNode; //***error -- non-lvalue in[/color][/color]
      [SNIP][color=blue][color=green]
      >> Node2* getNext(){ return next;}
      >>
      >> Why Am I getting an error denoted at ***?[/color]
      >
      > Because you try to assign a new value to an unnamed temporary
      > variable.[/color]

      What is returned by getNext is a copy of what is stored in next, it is an
      unnamed copy: the return value. You will need to return a reference to that
      pointer to be able to assign to it.

      --
      WW aka Attila


      Comment

      • Thomas Matthews

        #4
        Re: Linked-List help

        Ying Yang wrote:[color=blue]
        > <snippet>
        >
        > class Node1
        > {
        > Node2* link;
        >
        > public:
        > void insertNode2(Nod e2* innerNode)
        > {
        > link->getNext() = innerNode; //***error -- non-lvalue in
        > assignment
        > }
        > };
        >
        > class Node2
        > {
        > Node2* next;
        >
        > public:
        > Node2* getNext(){ return next;}
        > };
        >
        > ---
        >
        > Why Am I getting an error denoted at ***?[/color]

        The method Node2::getNext returns a _copy_ of a pointer Node2::next.
        In Node1::insertNo de2, you are assigning innerNode to the copy
        of a pointer (returned by Node2::getNext) .

        The above code suggests that you are having problems with a
        linked list or tree data structure. Why are you using two Nodes?
        If you post a description of the bigger picture, we may be
        able to give better help, such as:

        For trees requiring inner and leaf nodes:
        1. Create a parent class containing common methods and
        members for nodes.
        2. Have leaf and inner nodes inherit from the parent.
        3. Let the tree be represented by pointers to the
        parent node.
        4. All pointers are pointers to the parent class.


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Ying Yang

          #5
          Re: Linked-List help

          > [SNIP][color=blue][color=green][color=darkred]
          > >> link->getNext() = innerNode; //***error -- non-lvalue in[/color][/color]
          > [SNIP][color=green][color=darkred]
          > >> Node2* getNext(){ return next;}
          > >>
          > >> Why Am I getting an error denoted at ***?[/color]
          > >
          > > Because you try to assign a new value to an unnamed temporary
          > > variable.[/color]
          >
          > What is returned by getNext is a copy of what is stored in next, it is an
          > unnamed copy: the return value.[/color]

          Right, you mean the memory address of what next is pointing to? even though
          the return type says it should return a pointer of type Node2?
          [color=blue]
          >You will need to return a reference to that
          > pointer to be able to assign to it.[/color]

          How about:

          Node2* temp = link->getNext();
          temp = innerNode;

          or how do I make getNext() return a pointer of type Node2?
          Node2* getNext()
          {
          ...
          }




          ---
          Outgoing mail is certified Virus Free.
          Checked by AVG anti-virus system (http://www.grisoft.com).
          Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003


          Comment

          • Karl Heinz Buchegger

            #6
            Re: Linked-List help



            Ying Yang wrote:[color=blue]
            >[color=green]
            > > [SNIP][color=darkred]
            > > >> link->getNext() = innerNode; //***error -- non-lvalue in[/color]
            > > [SNIP][color=darkred]
            > > >> Node2* getNext(){ return next;}
            > > >>
            > > >> Why Am I getting an error denoted at ***?
            > > >
            > > > Because you try to assign a new value to an unnamed temporary
            > > > variable.[/color]
            > >
            > > What is returned by getNext is a copy of what is stored in next, it is an
            > > unnamed copy: the return value.[/color]
            >
            > Right, you mean the memory address of what next is pointing to? even though
            > the return type says it should return a pointer of type Node2?[/color]

            Right. And a pointer to Node2 is just that: a number.
            You can't assign something to a number.
            [color=blue]
            >[color=green]
            > >You will need to return a reference to that
            > > pointer to be able to assign to it.[/color]
            >
            > How about:
            >
            > Node2* temp = link->getNext();[/color]

            temp now points to whatever getNext() also points ..
            [color=blue]
            > temp = innerNode;[/color]

            .... and now temp points to innerNode.

            But the thing in getNext() still points to the same thing.

            [color=blue]
            > or how do I make getNext() return a pointer of type Node2?[/color]

            That's not what you want to do. You don't want to return a
            pointer to Node2, for the simple fact that this would be useless
            for the caller if he wants to assign a new address.
            The solution has already been shown to you: return a reference
            to a pointer. Didn't you see it?

            Node2*& getNext()
            {
            ...
            }

            Since you don't seem to be very knowledged in pointers, pointers to pointers
            and/or references, let me suggest something different.
            In addition to having such a GetNext function, why don't you simply
            add a SetNext function to your class:

            class Node2
            {
            Node2* next;

            public:
            Node2* getNext() { return next;}
            void setNext( Node2* n ) { next = n; }
            };

            class Node1
            {
            Node2* link;

            public:
            void insertNode2(Nod e2* innerNode)
            {
            link->setNext( innerNode );
            }
            };

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Chris Dams

              #7
              Re: Linked-List help

              "Ying Yang" <YingYang@hotma il.com> wrote in message news:<3f5c9fa0_ 1@news.iprimus. com.au>...[color=blue]
              > <snippet>
              >
              > class Node1
              > {
              > Node2* link;
              >
              > public:
              > void insertNode2(Nod e2* innerNode)
              > {
              > link->getNext() = innerNode; //***error -- non-lvalue in
              > assignment
              > }
              > };
              >
              > class Node2
              > {
              > Node2* next;
              >
              > public:
              > Node2* getNext(){ return next;}
              > };[/color]

              The reason for this error was already explained by previous posters.
              To make your code work, I would suggest to add a declaration

              friend class Node1;

              into class Node2 (change the order in which the class appear to make
              this legal) and change the function into:

              void insertNode2(Nod e2* innerNode)
              { link->next=innerNode ;
              }

              Reason: You probably do not want a public method to return a reference
              to a private value because then it is not terribly private anymore.
              Furthermore, you might want to change the name Node2 into Node and
              Node1 into LinkedList.

              Good day,
              Chris Dams

              Comment

              Working...