简体   繁体   中英

A reference that is not to 'const' cannot be bound to a non-lvalue

Am struggling a bit with this.

Am declaring:

BYTE *pImage = NULL;

Used in call:

m_pMyInterface->GetImage(i, &imageSize, &pImage);

Visual C++ 2003 compiler error:

error C2664: 'CJrvdInterface::GetImage' : cannot convert parameter 3 from 'BYTE **__w64 ' to 'BYTE **& ' A reference that is not to 'const' cannot be bound to a non-lvalue

The method called is defined as:

void CMyInterface::GetImage(const int &a_iTileId, ULONG *a_pulImageSize, 
                            BYTE** &a_ppbImage)

Any help much appreciated, Bert

Because GetImage can modify it's third parameter, you need to give it something to modify:

BYTE **ppImage = &pImage;
m_pMyInterface->GetImage(i, &imageSize, ppImage);

It is possible that after your function returns, that &pImage and ppImage may no longer be the same (which also means that pImage and *ppImage may be different). If you add this:

if (ppImage)
    pImage = *ppImage;

after the call, you should be good.

If CMyInterface::GetImage is your own function, depending on what you do, you may be able to change it. In your function, do you ever do:

a_ppbImage = ...;

or do you only write:

*a_ppbImage = ...;

If you only do that latter and not the former, passing a reference to a double pointer is overkill. You can either pass a reference to a single pointer ( BYTE *&image ) or you can pass a double pointer ( BYTE **image )

If you are trying to modify the variable 'pImage' inside the method 'GetImage()' you should either be passing a pointer or a reference to it (not doing both).

What you probably want is:

BYTE *pImage = NULL;
x.GetImage(iTileId, pulImageSize, a_pImage );

With the method defined as:

void CMyInterface::GetImage(int const& a_iTileId, ULONG* a_pulImageSize, BYTE*& a_ppbImage)
{
}

PS. Be consistent where you put your & and * in type declarations.

ULONG   *a_pulImageSize   // Star on the right
BYTE**   &a_ppbImage      // Star on the left (not consistent)

Personally (and this is just my style others are different) I put everything on the left (with the type) just the variable name goes on the right.

You declared GetImage() to expect a reference to a Byte**.

void CMyInterface::GetImage(const int &a_iTileId, 
                            ULONG *a_pulImageSize,
                            BYTE** &a_ppbImage);

You passed it a reference to a Byte*.

BYTE *pImage = NULL;
m_pMyInterface->GetImage(i, &imageSize, &pImage);

To make your method call work as written, you need to change your definition of GetImage() to

void CMyInterface::GetImage(const int &a_iTileId, ULONG *a_pulImageSize,  
                            BYTE* &a_ppbImage) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM