### Problem with lvalue

Can someone tell me why I get an error message using temporary as lvalue [-fpermissive] on k.back().koef+=front().koef;

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 #include using namespace std; struct polinom { int koef; int stepen; }c; struct polinom1 { int koef; int stepen; }o; struct item { polinom val; item* next; }; class queue { private: item* first; item* last; public: queue(); ~queue(); void push(polinom c); void pop(); bool empty(); polinom front(); polinom back(); void read(int &n); void izhod(int n); void sort(int n); void subirane(int n); }; int main() { int n=0; queue a; a.read(n); a.sort(n); cout<<" "<val=c; p->next=NULL; if(first == NULL) { first = p; last=first; } else { last->next = p; last=p; } } queue::queue() { first = NULL; last = NULL; } queue::~queue() { while(first!=NULL) { item *temp=first; first=first->next; delete temp; } } void queue::pop() { if(first!=NULL) { item *temp=first; first=first->next; delete temp; if(first==NULL) { last=NULL; } } } bool queue::empty() { return last==NULL; } polinom queue::front() { if(first != NULL) return first->val; } polinom queue::back() { if(last != NULL) return last->val; } void queue::read(int &n) { while(cin) { cout<<"Vavedete koeficient "; cin>>c.koef; if(!cin) break; cout<<"Vavete stepen "; cin>>c.stepen; push(c); n++; } cin.clear(); } void queue::izhod(int n) { int br=0; polinom d; while(!empty()) { d=front(); br++; if(empty()) break; if(br
polinom front();
^^ This is why. Front() should return a reference, not a copy.

try
polinom& front();
Same with back()
Here's a minimal program that also makes the same error message, in case it's still confusing:
 1234567891011121314 // Example program struct Foo { int bar; }; Foo func() { return Foo(); // returning a temporary value } int main() { func().bar += 3; // trying to increment a temporary value. }

 13:16: error: using temporary as lvalue [-fpermissive]

Aside from the fact that you're not returning references, another issue with these functions is that not all control branches return a value.
 12345678910 polinom queue::front() { if(first != NULL) return first->val; } polinom queue::back() { if(last != NULL) return last->val; }

Nothing is returned if last == NULL... And you can't have null references by design in C++. There's no perfect solution to this. You could throw an exception. The standard library's containers will just create undefined behavior if you try to access an empty container's back() or front().

http://www.cplusplus.com/reference/vector/vector/front/
 Exception safety If the container is not empty, the function never throws exceptions (no-throw guarantee). Otherwise, it causes undefined behavior.

Edit:
 12345678910 struct polinom { int koef; int stepen; }c; struct polinom1 { int koef; int stepen; }o;

Please note there that you're creating global objects for polinom and polinom1, which you don't even use, and also these are shadowed by the inputs to your functions (like queue::push).
In general, you should avoid using global variables. And use DESCRIPTIVE variable names. "o" and "c" are not good variable names. Also, the difference between "polinom" and "polinom1" is non-existent as far as the data goes, why do you have two structs?
Last edited on
Registered users can post here. Sign in or register to post.