Error: Cannot Bind Non-Const Lvalue Reference of Type 'Int&' to an Rvalue of Type 'Int'

cannot bind non-const lvalue reference of type 'Node&' to an rvalue of type 'const Node'

The problem is that the function delete_node has its first parameter as a reference to non-const Node while the function get_node returns a Node by value. This means that the call expression get_node(2, &node1) is an rvalue. But since, we cannot bind a reference to non-const Node to an rvalue of type Node, you get the mentioned error.

One way to solve this is to change the return type of get_node to Node& as shown below:

//--vvvvv------------------------------------------------>return type changed to Node&
Node& get_node(size_t position, Node *current_node){
//other code as before
return *current_node;
}

Why does this code give the error cannot bind non-const lvalue reference of type ‘char*&’ to an rvalue of type ‘char*’

The reason is that the C++ standard doesn't allow non-const references to bind to temporaries, and std::string::data returns a pointer by value. Only const reference can do that, and prolong the life of the temporary object.

In your case you either need to make your reference const.

const auto& r = p.data();

Or better, just create a variable that will store the pointer for you, as pointers are cheap to copy around.

const char* r = p.data();

cannot bind non-const lvalue reference of type to an rvalue of type

Create_moneys() is a function that takes a mutable reference to a pointer.

What this means is that it's technically possible for the function to modify the pointer itself in a way that gets propagated to the caller.

For example:

int global_x;
void foo(int*& ptr) {
ptr = &global_x;
}

void bar() {
int local_x;
int * local_ptr = &local_x;
foo(local_ptr);
//local_ptr now points to global_x!
}

Now, pm1 is an array, and when you call Create_moneys(pm1); the language creates a temporary pointer that points to the start of the array. That temporary value is called a RValue, and RValues are unmodyfiable constants, so you are not allowed to use them as mutable reference arguments.

The fix is simple, since Create_moneys() does not modify the pointer, it has no reason to take its argument by reference, just declare it as Create_moneys(shared_ptr<CoinMoney>* ms) instead.

cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'

The problem is not related to forward.

In the call add2T(x), the deduced template argument T is int&. (Only in this way can T&& be an lvalue reference type.) Thus the return type is also int&. However, the operand of return (namely add2(std::forward<T>(x))) is an rvalue that cannot be used to initialize int&. Hence the error message.

If you want to prevent the return type from becoming a reference type, you can apply std::decay_t:

template <typename T>
std::decay_t<T> add2T(T&& x)

Non const lvalue references

That is because a temporary can not bind to a non-const reference.

double &m = a;

a is of type int and is being converted to double. So a temporary is created. Same is the case for user-defined types as well.

Foo &obj = Foo(); // You will see the same error message.

But in Visual Studio, it works fine because of a compiler extension enabled by default. But GCC will complain.

std::tie fails with cannot bind non-const lvalue reference when passed value from a function call

std::tie takes lvalue references as arguments, so int returned by S::y can't bind. You could use the perfect forwarding version, std::forward_as_tuple, instead:

#include <tuple>

struct S
{
int x = 0;
int y() const { return 1; }
};

bool f(const S& a, const S& b)
{
return std::forward_as_tuple(a.x, a.y()) < std::forward_as_tuple(b.x, b.y());
}

Demo.

non-const lvalue reference to type ... cannot bind to a temporary of type

You can't.

The C++ standard does not allow the binding of an anonymous temporary to a reference, although some compilers allow it as an extension. (Binding to a const reference is allowed.)

Aside from the workaround you already have, if you can change the function to take const QImage& then that would be better.

operator overloading Error: cannot bind non-const lvalue reference of type ‘some-type’ to an rvalue of type ‘some-type’

Your constructors and operator overloads taking ComplexNumber& as argument need to take const ComplexNumber& instead.

This line ComplexNumber sum = c1 + c2 actually calls your ComplexNumber copy constructor and gives it the c1 + c2 expression result, which is a temporary ComplexNumber. Since you can't make a non-const reference to a temporary object in C++, you get this error.

Ideally your ComplexNumber class should look like this:

struct ComplexNumber {

// ...

// Copy
ComplexNumber(const ComplexNumber& other) { *this = other; }
ComplexNumber& operator=(const ComplexNumber& other) {
if (this != &other) { /* copy your attributes from other to this */ }
return *this;
}

// Move (optional - and not really useful in your case)
ComplexNumber(ComplexNumber&& other);
ComplexNumber& operator=(ComplexNumber&& other);

// Unlike operator= we return a temporary value,
// so it's a ComplexNumber indeed (no &)
// We'll modify neither 'this' nor 'rhs', so mark them const
ComplexNumber operator+(const ComplexNumber& rhs) const;

~ComplexNumber();
};

More details: https://en.cppreference.com/w/cpp/language/rule_of_three


Some tips:

  • Don't put using namespace std in headers (I guess it's a header)
  • typedef struct _Data { ... } Data; is the old C notation. You can simply use struct Type { ... } myObj;

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;


Related Topics



Leave a reply



Submit