#include <iostream>
usingnamespace std;
class node
{
private:
int key;
node * next;
public:
node(int);
int getkey();
node * getnext();
void setnext(node n);
void print();
};
node * node::getnext()
{
return (next);
}
void node::setnext(node n)
{
next = &n;
}
void node::print()
{
cout<<key<<endl;
}
node::node(int a)
{
key =a;
}
int node::getkey()
{
return (key);
}
class head
{
private:
node * head;
public:
bool find(int);
void p();
void sethead(node);
void insert(node* nptr);
};
void head::insert(node* nptr)
{
node* tptr = head;
while (tptr->getnext() != NULL)
{
tptr= tptr->getnext();
}
tptr->getnext() = nptr; //causing error :: In member function `void head::insert(node*)':|
//|66|error: non-lvalue in assignment|
//|67|error: non-lvalue in assignment|
//||=== Build finished: 2 errors, 0 warnings ===|
nptr->getnext() = NULL;
}
void head::sethead(node p)
{
head =&p;
}
bool head::find(int k)
{
node * lct = head;
while (lct != NULL)
{
if(lct->getkey() == 9)
{
cout <<"found"<<endl;
break;
}
else cout <<"not found"<<endl;
lct =lct->getnext();
}
}
int main ()
{
node a(9);
node c(10);
head b;
node *h=&c;
b.sethead(a);
int o =10,l =90;
int *i = &l;
int *p = &o;
b.insert(h);
b.find(10);
}
i am not sure why i am getting an error, sorry for asking this but i am kinda lost. but assignment in line 66 and 67 are valid, i am assigning pointer to a pointer then, a pointer to NULL.
You can't assign to a value returner by a function. Function just creates a copy. It is useless to assign anything to it.
However, if the function returns a pointer, you can assign to the object it is pointing to. Here you need to assign a node*, so the function will have to return node**. Then (*tptr->getnext()) = 0;//I'm not sure if you need the parenthesis here will be fine.
References are a way to simplify the problem. It works like a pointer, but looks like a normal variable. If your function returned node*&, you could simply do tptr->getnext() = 0;.