Linked-List help

Discussion in 'C++' started by Ying Yang, Sep 8, 2003.

  1. Ying Yang

    Ying Yang Guest

    <snippet>

    class Node1
    {
    Node2* link;

    public:
    void insertNode2(Node2* 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
    Ying Yang, Sep 8, 2003
    #1
    1. Advertising

  2. Ying Yang

    White Wolf Guest

    Ying Yang wrote:
    > <snippet>
    >
    > class Node1
    > {
    > Node2* link;
    >
    > public:
    > void insertNode2(Node2* 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 ***?


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

    --
    WW aka Attila
    White Wolf, Sep 8, 2003
    #2
    1. Advertising

  3. Ying Yang

    White Wolf Guest

    White Wolf wrote:
    [SNIP]
    >> link->getNext() = innerNode; //***error -- non-lvalue in

    [SNIP]
    >> 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.


    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
    White Wolf, Sep 8, 2003
    #3
  4. Ying Yang wrote:
    > <snippet>
    >
    > class Node1
    > {
    > Node2* link;
    >
    > public:
    > void insertNode2(Node2* 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 ***?


    The method Node2::getNext returns a _copy_ of a pointer Node2::next.
    In Node1::insertNode2, 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:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Sep 8, 2003
    #4
  5. Ying Yang

    Ying Yang Guest

    > [SNIP]
    > >> link->getNext() = innerNode; //***error -- non-lvalue in

    > [SNIP]
    > >> 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.

    >
    > What is returned by getNext is a copy of what is stored in next, it is an
    > unnamed copy: the return value.


    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?

    >You will need to return a reference to that
    > pointer to be able to assign to it.


    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
    Ying Yang, Sep 9, 2003
    #5
  6. Ying Yang wrote:
    >
    > > [SNIP]
    > > >> link->getNext() = innerNode; //***error -- non-lvalue in

    > > [SNIP]
    > > >> 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.

    > >
    > > What is returned by getNext is a copy of what is stored in next, it is an
    > > unnamed copy: the return value.

    >
    > 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?


    Right. And a pointer to Node2 is just that: a number.
    You can't assign something to a number.

    >
    > >You will need to return a reference to that
    > > pointer to be able to assign to it.

    >
    > How about:
    >
    > Node2* temp = link->getNext();


    temp now points to whatever getNext() also points ..

    > temp = innerNode;


    .... and now temp points to innerNode.

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


    > or how do I make getNext() return a pointer of type Node2?


    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(Node2* innerNode)
    {
    link->setNext( innerNode );
    }
    };

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 9, 2003
    #6
  7. Ying Yang

    Chris Dams Guest

    "Ying Yang" <> wrote in message news:<>...
    > <snippet>
    >
    > class Node1
    > {
    > Node2* link;
    >
    > public:
    > void insertNode2(Node2* innerNode)
    > {
    > link->getNext() = innerNode; //***error -- non-lvalue in
    > assignment
    > }
    > };
    >
    > class Node2
    > {
    > Node2* next;
    >
    > public:
    > Node2* getNext(){ return next;}
    > };


    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(Node2* 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
    Chris Dams, Sep 9, 2003
    #7
    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. Chris Ritchey
    Replies:
    7
    Views:
    463
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    453
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    491
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    652
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    733
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page