• Register
0 votes
799 views

Problem :

Below is the wrong form:

int &z = 12;

Following is the correct form:

int y;
int &r = y;

Question:
Why is a first code wrong? What is a "meaning" of a below error?

“Invalid initialization of non-const reference of type from an rvalue of type”

7 5 2
3,870 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

C++03 3.10/1 says that: "Every expression is either the lvalue or the rvalue." It is very important to remember that the lvalueness versus the rvalueness is the property of expressions and not of the objects.

Lvalues name objects that persist beyond the single expression. For example, obj , *ptr , ptr[index] , and ++x are all of the lvalues.

Rvalues are the temporaries that evaporate at a end of the full-expression in which they live ("at a semicolon"). For example, 1729 , x + y , std::string("meow") , and x++ are all of the rvalues.

A address-of the operator requires that its "operand shall be the lvalue". if we could take a address of one of the expression then the expression is the lvalue, otherwise it is termed as the rvalue.

 &obj; //  it is valid
 &12;  //it is invalid

 OR

12 is the compile-time constant which can not be changed unlike a data referenced by the int&. What you can do is as below

const int& z = 12;
9 7 4
38,600 points
0 votes

Solution:

C++03 3.10/1 says: "Every expression is either an lvalue or an rvalue." It's important to remember that lvalueness versus rvalueness is a property of expressions, not of objects.

Lvalues name objects that persist beyond a single expression. For example, obj , *ptr , ptr[index] , and ++x are all lvalues.

Explanation:

Rvalues are temporaries that evaporate at the end of the full-expression in which they live ("at the semicolon"). For example, 1729 , x + y , std::string("meow") , and x++ are all rvalues.

The address-of operator requires that its "operand shall be an lvalue". if we could take the address of one expression, the expression is an lvalue, otherwise it's an rvalue.

&obj; //  valid
 &12;  //invalid

10 is a constant, so you can't pass a reference to it, simply because the whole concept of changing a constant is bizarre.

References were introduced to solve one of the thorny problems in C (and earlier C++), the fact that everything is passed by value and, if you want to have a change reflected back to the caller, you have to pass in a pointer and dereference that pointer within the function to get at the actual variable (for reading and writing to it).

This is something that would be seriously good to have in the next ISO C standard. While having to use pointers may give some of us a lot of rep on Stack Overflow, it's not doing the C programmers of the world much good :-)

The solution to your problem is simple. If you don't need to change the item in the function, just pass it normally:

int fun (int x) { ... }

10 is a constant, so you can't pass a reference to it, simply because the whole concept of changing a constant is bizarre.

References were introduced to solve one of the thorny problems in C (and earlier C++), the fact that everything is passed by value and, if you want to have a change reflected back to the caller, you have to pass in a pointer and dereference that pointer within the function to get at the actual variable (for reading and writing to it).

This is something that would be seriously good to have in the next ISO C standard. While having to use pointers may give some of us a lot of rep on Stack Overflow, it's not doing the C programmers of the world much good :-)

The solution to your problem is simple. If you don't need to change the item in the function, just pass it normally:

int fun (int x) { ... }

If you do need to change it, well, then you'll have to pass something that can be changed:

int xyzzy = 10;
cout << fun (xyzzy);

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 6 views
6 views
Problem: Why I got the error > Non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'
asked Apr 26 ummeshani 10.8k points
0 votes
1 answer 5 views
5 views
Problem: I'm using C++ (not C++11). I need to make a pointer to a function inside a class. But there's an error: "Reference to nonstatic member function must be called". What should I do to make a pointer to a member of MyClass?
asked Mar 30 ummesalma 29.2k points
0 votes
1 answer 4 views
4 views
Problem: is this allowed to pass an array by reference ?What is the proper way to pass an array by reference? Or a work around?
asked Apr 18 ummesalma 29.2k points
0 votes
1 answer 4 views
4 views
Problem: please to solve it ... pass array as reference c++
asked Apr 4 Ifra 37.2k points
1 vote
1 answer 6 views
6 views
Problem : Any solution for this please: Static member cannot be accessed with an instance reference.
asked May 1 PkGuy 23.5k points
0 votes
1 answer 8 views
8 views
Problem: I know references are syntactic sugar, so code is easier to read and write. But what are the differences?
asked Apr 24 sumaiya simi 43.9k points
0 votes
1 answer 19 views
19 views
Problem: I need someone&rsquo;s help to fix it... if p is a pointer variable, the statement p = p + 1; is valid in c++.
asked Mar 23 Ifra 37.2k points
0 votes
1 answer 7 views
7 views
Problem: Hello! I am confused regarding the use of pointers and references. I want to know where to use pointers and where to use the reference. What difference does it make if I use pointers instead of references? Thanks!
asked Mar 23 Code Learner 9.9k points
0 votes
1 answer 3 views
3 views
Problem: Which of the following can have a pointer as an operand?
asked 1 day ago Junia Phoebe 15.3k points
1 vote
1 answer 2K views
2K views
Problem: I am very new to Programming. I want to compile my recently written program it is called myes3 but when I try to write from the terminal: gcc myes3.c -o myes3 I always face below error message: /usr/lib/gcc/i686-linux-gnu/4.4.5/../../. ... collect2: ld returned 1 exit status I have very elementary knowledge of programming so unable to fix above error. Please help me in fixing above error.
asked May 27, 2020 Martin K 6.6k points