Linked-List help

Y

Ying Yang

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

White Wolf

Ying said:
<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;}
};

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

White Wolf

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

Thomas Matthews

Ying said:
<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 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
 
Y

Ying Yang

[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()
{
...
}
 
K

Karl Heinz Buchegger

Ying said:
[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.
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 );
}
};
 
C

Chris Dams

Ying Yang said:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top