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
kb******@gascad.at