• Register
0 votes
150 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

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

1 vote
1 answer 283 views
283 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 Martin K 6.6k points
1 vote
1 answer 75 views
75 views
Problem: Hello Kodlogs, I am going through a problem for the last few minutes. I tried to solve it in many ways but still couldn&rsquo;t get rid of this. I&rsquo;ve learning c++ programming language from last week. So, you can treat me as a novice in it. I have a ... (addr,retstring().c_str()); And the function I tried to call: void Printfunc(int a, char *loc, char *stream) Thanks. Good day!
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 43 views
43 views
Problem : I am trying to run some of the unit tests in the C# Windows Forms application (Visual Studio 2005), however I am facing the below error : System.IO.FileLoadException: Could not load file or assembly 'Utility, Version=1.2.0.200, Culture=neutral, PublicKeyToken= ... version 1.2.0.203. Any pointers on how can I figure out what is trying to reference the old version of my DLL file?
asked Dec 23, 2019 alecxe 7.5k points
0 votes
1 answer 4 views
0 votes
1 answer 37 views
37 views
Problem: I have written the following class: class Foo { private:     static float scale; public:     static float setScale(float scale); }; If I try to implement the setScale() function as shown below: float Foo::setScale(float scale) {    this->scale = ... still throws the same error. Now I am really confused about the above error message as my function in question is already marked as static.
asked Aug 13 Raphael Pacheco 4.9k points