mercredi 26 février 2020

"initial value of reference to non-const must be an lvalue" with overloading + for concatenating vectors

void print(/* const  */ vector<int> &A)
{
   for (int i = 0; i < A.size(); i++)
      cout << A[i] << " ";
   cout << endl;
}

vector<int> operator+(vector<int> &a, vector<int> &b)
{
   vector<int> ret = a;
   ret.insert(ret.end(), b.begin(), b.end());
   return ret;
}

int main(){
     vector<int> A = {1,1,1}
     vector<int> D = {1,1}
     print(A + B)

}

Without decommenting const it says

initial value of reference to non-const must be an lvalue

why is that so ? if operator returns non-const vector and print accepts non-const ?

Aucun commentaire:

Enregistrer un commentaire