Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'&' requires l-value on &std::unique_ptr<>.get

I am trying to use the function NuiCreateSensorByIndex(int,INuiSensor**). I am trying not to use naked pointers, so I did std::unique_ptr<INuiSensor> nui; to make it an unique_ptr. Now I want to acces this function, so I do the following: hr = NuiCreateSensorByIndex(i, &nui.get());, but this is wrong:

KinectManager.cpp:29: error: C2102: '&' requires l-value

What am I doing wrong and how to fix it?

like image 903
Cheiron Avatar asked Dec 03 '25 10:12

Cheiron


2 Answers

The compiler is right: although std::unique_ptr<INuiSensor> can be used to point to things, it is not an object a pointer to which is expected by the NuiCreateSensorByIndex(int,INuiSensor**) function. The reason the function wants a pointer to a pointer is that it wants to modify pointer's content by an assignment of this sort:

*ptrToPtr = somePtr;

If compiler let you pass a pointer to std::unique_ptr<INuiSensor>, this assignment would be invalid. That's why you need to create a temporary "naked" pointer, pass it to the function, and then assign the result back to std::unique_ptr<INuiSensor>.

like image 187
Sergey Kalinichenko Avatar answered Dec 04 '25 23:12

Sergey Kalinichenko


The purpose of unique_ptr is to take control of ownership of resource. The pointer to pointer is to modify the address of the resource, which will change the ownership of the resource. One solution is to release the ownership first, then reclaim it, such as:

auto p = uni.release();
NuiCreateSensorByIndex(0, &p);
uni.reset(p);
like image 37
Changjiang Yang Avatar answered Dec 05 '25 00:12

Changjiang Yang