Problem with lvalue

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  #include<iostream>
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<<" "<<endl;
    a.subirane(n);
    a.izhod(n);
}
void queue::push(polinom c)
{
    item* p= new item;
    p->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<n)
        {
         cout<<d.koef<<"*x^"<<d.stepen<<"+";
        }
        else
        {
         cout<<d.koef<<"*x^"<<d.stepen;
         break;
        }
        pop();
    }
}
void queue::sort(int n)
{
    polinom o[20],swap;
    polinom d;
    int k=0;
    for(int i=0;i<n;i++)
    {
       d=front();
       o[i]=d;
       pop();
    }
    for(int z=0;z<n;z++)
    {
        cout<<"*"<<o[z].stepen<<"*";
    }
    for (int i=0;i<n-1;i++)
     for (int j=0;j<n-i-1;j++)
        if (o[j].stepen<o[j+1].stepen)
        {
           swap=o[j];
           o[j]=o[j+1];
           o[j+1]=swap;
        }
    for(int p=0;p<n;p++)
    {
        push(o[p]);
    }
}
void queue::subirane(int n)
{
    queue k;
    k.push(front());
    pop();
    while(!empty())
    {
        if(k.back().stepen==front().stepen)
        {
            k.back().koef+=front().koef;
        }
        else
        {
            k.push(front());
        }
        pop();
    }
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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.
1
2
3
4
5
6
7
8
9
10
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:
1
2
3
4
5
6
7
8
9
10
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
Topic archived. No new replies allowed.