initial value of reference to non-const must be an lvalue
When you pass a pointer by a non-const
reference, you are telling the compiler that you are going to modify that pointer's value. Your code does not do that, but the compiler thinks that it does, or plans to do it in the future.
To fix this error, either declare x
constant
// This tells the compiler that you are not planning to modify the pointer
// passed by reference
void test(float * const &x){
*x = 1000;
}
or make a variable to which you assign a pointer to nKByte
before calling test
:
float nKByte = 100.0;
// If "test()" decides to modify `x`, the modification will be reflected in nKBytePtr
float *nKBytePtr = &nKByte;
test(nKBytePtr);
initial value of reference to non-const must be an lvalue, Passing an object type by reference error
The function GetStart returns a temporary object of the type Point:
Point GetStart();
while the function OffsetVector excepts a non-constant reference to an object:
void OffSetVector(Point&,int,int);
You may not bind a temporary object with a non-constant lvalue reference.
Change the declaration of the function GetStart like:
Point & GetStart();
Also at least the function GetEnd should be changed like:
Point & GetEnd();
You could overload the functions for constant and non-constant objects:
Point & GetSgtart();
cosnt Point & GetStart() const;
Point & GetEnd();
const Point & GetEnd() const;
initial value of reference to non-const must be an lvalue can be valid when returning from function?
The error message makes it quite clear:
initial value of reference to non-const must be an lvalue
(emphasis mine). So long as the reference is initially bound to an l-value, everything is fine (so long as you don't use a reference to a stack local variable, of course). The reference returned from get_value
is bound to x
which is an l-value, and that's allowed.
Without the function, you are essentially writing:
int x = 10; // x is an l-value
int &get_x = x; // just a variable instead of a function
get_x = 20; // assignment is ok
which is clearly ok.
C++ How to fix error initial value of reference to non-const must be an lvalue?
void CAnimation::Update(float deltaTime)
{
const FrameData* data = currentAnimation.second->GetCurrentFrame();
}
Initial value of reference to non-const must be an lvalue when passing opencv Mat to a function
Doesn't this work for you?
/* Renamed arg to reflect what's happening.
Let it be A if you so wish but it's not
the original A from temp().
*/
void temp2(Mat &extractedFromA){
// Do stuff
}
void temp1(Mat &A){
Point a(0,0), b(10,10);
Rect t(a,b);
Mat extracted = A(t);
temp2(extracted);
}
You are using this API
Mat cv::Mat::operator() (const Rect & roi)const
which means that on calling A(t)
, A
is unmodified (since above API is a const method) and yields a new Mat
object and this is what you need to operate on in temp2()
, not the original object A
passed into temp1()
. Also the changes made to the extracted
Mat
shall be reflected back to the original Mat A
since you're only sharing the headers between them.
Also, the error that you might be hitting into is that since A(t)
was yielding a temporary object, and on passing it to temp2()
, you were attempting to bind it to a non-const lvalue reference. Making it as a const lvalue reference would fix it but that obviously won't help you.
C++ what does the error of Initial value of reference to a non-const must be an lvalue mean in this case?
The error is from this line:
void print_vector(std::vector<int>& v) {
Since you didn't include the const
keyword in the argument-type, you are (implicitly) indicating that print_vector
has the right to modify the contents of v
.
However, you are calling print_vector()
with a temporary object (the vector returned by factorize()
) as an argument, and C++ doesn't allow you to pass a temporary object by non-const reference, presumably on the theory that making changes to a temporary object is pointless (because the temporary is going to be destroyed as soon as the function call returns, so any changes made to it would have no effect) and therefore must a programmer error.
In any case, the fix is easy, just change your function declaration to this:
void print_vector(const std::vector<int>& v) {
... and that will allow you to pass a reference-to-a-temporary-vector to it.
C++/SDL initial value of reference to a non-const must be an lvalue
Renderer::Draw
declares to modify its second argument (otherwise it would be const and you wouldnt get that error), if you pass a temporary then modifying the argument is pointless and it is most likely an error, thats why it is forbidden.
Consider this example:
int foo() { return 3; }
void bar(int& x) { ++x; }
void moo(const int& x) {}
int main() {
bar(foo()); // not allowed
moo(foo()); // ok
int x = foo();
bar(x); // ok, modifies x
}
If bar(foo())
would be allowed, nothing super bad would happen. On the other hand there is no reason to allow it. bar
explicitly declares that it wants to modify its argument so why allow to pass something when you have no way to observe that modification?
PS: If you think this answer is too handwavy, may I refer you to the comment by @DanielLangr:
Short answer: Because lvalue references cannot bind rvalues.
Related Topics
How to Compare Two Standard Conversion Sequences Use the Rank of Contained Conversions
C++ Passing an Array Pointer as a Function Argument
What Is the Purpose of Forward Declaration
Windows Media Foundation Recording Audio
Generic Way to Cast Int to Enum in C++
Error: "No Match for Operator+" , for List Iterator
Getting a List of User Profiles on a Computer in C++ Win32
How Do C++ Progs Get Their Return Value, When a Return Is Not Specified in the Function
How the New Range-Based for Loop in C++17 Helps Ranges Ts
C++ CSV Line with Commas and Strings Within Double Quotes
How Does Url Within Function Body Get Compiled
How to Test Whether a Number Is a Power of 2
What's the Difference Between Cstdlib and Stdlib.H
Stl Vector: Moving All Elements of a Vector
C++: Difference Between Member and Non Member Functions
Is There a Case Where Including the Same Header Twice Is Actually Helpful