Home > Software engineering >  No match for ‘boost::shared_ptr::operator=’
No match for ‘boost::shared_ptr::operator=’

Time:05-11

This is the code I have that causes the error below:

class CAlternateMerchantList
{
public:
    CAlternateMerchant::SP m_pAlternateMerchantList[MAX_PLAYER_LIST];
    int m_nMax;
    int m_nCur;

    CAlternateMerchantList()
    {
        int i;
        for (i = 0; i < MAX_PLAYER_LIST; i  )
            m_pAlternateMerchantList[i] = NULL;
        m_nMax = 0;
        m_nCur = 0;
    }

The error I get is the following:

PersonalShop.h: In constructor ‘CAlternateMerchantList::CAlternateMerchantList()’:
PersonalShop.h:227: error: no match for ‘operator=’ in ‘((CAlternateMerchantList*)this)->CAlternateMerchantList::m_pAlternateMerchantList[i] = 0’
/usr/local/include/boost-1_65_1/boost/smart_ptr/shared_ptr.hpp:547: note: candidates are: boost::shared_ptr<T>& boost::shared_ptr<T>::operator=(const boost::shared_ptr<T>&) [with T = CAlternateMerchant]

As you can see, I'm using boost 1_65_1 libraries. If I am not wrong, this code worked on another system with boost 1_59, but at the moment I can not access it for testing.

Does anyone know how to make this code work with boost 1.65? Or, is there any other issue here?

CodePudding user response:

You don't need to set boost::shared_ptrs to null. They have a default constructor which does it automatically. You can simply delete the entire for loop.

I suggest also using an initialization list for m_nMax and m_nCur.

CAlternateMerchantList()
    : m_nMax(0), m_nCur(0)
{
}

CodePudding user response:

You are likely compiling for an older C version, as evident by your compiler not understanding what nullptr is.

Prior to C 11, NULL is an alias for an integer literal 0, which can be assigned to raw pointers, but boost::shared_ptr can't be assigned an integer.

In C 11 and later, NULL is (typically) an alias for the nullptr keyword, and boost::shared_ptr can be assigned, and constructed from, nullptr.

In any case, as John Kugelman explained, you don't need to initialize your array's smart pointer elements to NULL/nullptr to begin with, as they are already initialized to that state for you by boost::shared_ptr's default constructor. So just remove your for loop altogether.

  • Related