not understanding the error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

not understanding the error

void remove(float a[], int& n, int i); int main() { float a[]={2,3,4,5,6}; cout<<"Removing: "; remove(a,5,3); //error in this line } void remove(float a[], int& n, int i) { for(int j=i+1; j<n; j++) { a[j-1]=a[j]; --n; } } this code has an error like bind non const 1value reference of type int& to an rvalue of type int...how can I fix the error?

22nd Apr 2021, 7:34 PM
Ramisa Fariha
Ramisa Fariha - avatar
3 Answers
+ 1
Pass 'n' by value, i.e. int n, if you don't need to store anything in it for the caller. The problem is that a literal like 5 can not be bound to a reference. I also don't quite understand why you decrement 'n' in the loop if you want to shift the elements left.
22nd Apr 2021, 7:38 PM
Shadow
Shadow - avatar
+ 1
Adding more to Shadow's answer, to fix this: remove & from the "int& n" from the function remove() declaration
22nd Apr 2021, 7:43 PM
Rohit
+ 1
thank you
22nd Apr 2021, 7:45 PM
Ramisa Fariha
Ramisa Fariha - avatar