4

I have this problem

83:24: error: non-const lvalue reference to type 'QImage' cannot bind to a temporary of type 'QImage' cameraimplementation.h:23:34: note: passing argument to parameter 'nextImage' here

caused by this code

updateImageData(toQImage()); 

with

void updateImageData(QImage& nextImage); QImage toQImage(); 

How can I solve this other than including a temporary variable.

QImage image = toQImage(); updateImageData(image); 
4
  • 4
    why would you want to write updateImageData(toQImage()); ? It updates an image that will be gone on the next line Commented Aug 31, 2018 at 12:55
  • 2
    What are you trying to achieve? Construct a temporary image, update it, and then discard it? C++ doesn't let you, because such a thing has a substantial chance of being a mistake. Commented Aug 31, 2018 at 12:55
  • in a world where you have millions of possibilities to shoot yourself in the foot it can be confusing when you find one way that is not allowed :P Commented Aug 31, 2018 at 12:56
  • The image in the class is updated by the passed one, not the image passed to the function. Mabye saveImageData would be better? Commented Aug 31, 2018 at 20:15

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. updateImageData creates a copy of the QImage. It is thus valid to pass it as const.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.