RonaldVG
RonaldVG

Reputation: 11

A bit complex C2102: '&' requires l-value error

I'm trying to compile a old C++ code (Eudora Mail) on Visual Studio 2017 and I'm getting about 400 C2102 errors.

The code is very similar in all the cases:

        const CSummary *&       GetPrev(POSITION& rPosition) const
                            { return reinterpret_cast<const CSummary *&>( m_ObList.GetPrev(rPosition) ); }  // Error C2102

<afxcoll.inl>

    _AFXCOLL_INLINE const CObject* CObList::GetPrev(POSITION& rPosition) const // return *Position--
{ CNode* pNode = (CNode*) rPosition;
    ASSERT(AfxIsValidAddress(pNode, sizeof(CNode)));
    if( pNode == NULL )
        AfxThrowInvalidArgException();
    rPosition = (POSITION) pNode->pPrev;
    return pNode->data; }

Can anyone help me to find a easy way to solve them ?

Thank you in advance

Ronald

Upvotes: 1

Views: 339

Answers (2)

RonaldVG
RonaldVG

Reputation: 11

I've changed:

const CSummary*& GetHead() const
                            { return reinterpret_cast<const CSummary *&>( m_ObList.GetHead() ); }

To:

const CSummary*& GetHead() const
{
    const CObject* tmp;
    tmp = m_ObList.GetHead();
    return reinterpret_cast<const CSummary *&>(tmp);} 

m_ObList.GetHead is something like this:

    _AFXCOLL_INLINE const CObject* CObList::GetHead() const
{ ASSERT(m_pNodeHead != NULL);
    return m_pNodeHead->data; }

So I got no compiler errors, but I'm not sure if it is what it should be

Upvotes: 0

StPiere
StPiere

Reputation: 4253

From the docs:Compiler Error C2102

The address-of operator ( & ) must have an l-value as operand.

The errors comes because you're getting adress of a temporary:

m_ObList.GetPrev(rPosition)

which is a non-const pointer of type const CObject*.

One cannot bind non-const lvalue reference to temporary.

If you want to return a pointer you can remove &:

 CSummary* GetPrev(POSITION& rPosition) const
 { return static_cast<const CSummary *>( m_ObList.GetPrev(rPosition) ); }

Upvotes: 2

Related Questions