c++ - Invalid initialization of non-const reference of type -


in following code, i'm not able pass temporary object argument printage function:

struct person {   int age;   person(int _age): age(_age) {} };  void printage(person &person) {    cout << "age: " << person.age << endl; }  int main () {   person p(50);   printage(person(50));  // fails!   printage(p);   return 0; } 

the error is:

error: invalid initialization of non-const reference of type ‘person&’ rvalue of type ‘person’ 

i realize passing lvalue function expecting rvalue... there way convert lvalue rvalue using std::move or something? tried taking constant parameter, not seem work.

simply make print function take argument const&. logically right doesn't modify argument.

void printage(const person &person) {    cout << "age: " << person.age << endl; } 

the actual problem other way around. passing temporary(rvalue) function expects lvalue.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -