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?
3 Respostas
+ 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.
+ 1
Adding more to Shadow's answer, to fix this:
remove & from the "int& n" from the function remove() declaration
+ 1
thank you