Search code examples
c++type-conversionnullptr

Assigning to DWORD from incompatible type nullptr_t


Despite reading several internet and forum questions/answers I do not yet understand why the following code generates an "assignment from incompatible pointer type" error.

void ITEM_MANAGER::FindItemMonster(LPCHARACTER ch, std::string name_item){
    DWORD iGasite = 1;
    int iIndex = 0;
    itertype(m_map_pkDropItemGroup) it;

    for (std::map<DWORD, CDropItemGroup*>::iterator it = m_map_pkDropItemGroup.begin(); it != m_map_pkDropItemGroup.end(); it++)
    {
        if (it != m_map_pkDropItemGroup.end())
        {
            __typeof(it->second->GetVector()) v = it->second->GetVector();

            for (DWORD i = 0; i < v.size(); ++i)
            {
                TItemTable* Titem = ITEM_MANAGER::instance().GetTable(v[i].dwVnum);
                bool nume = (name_item.length() ? strstr(Titem->szLocaleName, name_item.c_str()) != NULL : true);
                const CMob* pMob = CMobManager::instance().Get(it->first);
                std::string name_monster(pMob->m_table.szLocaleName);
                if (nume)
                {
                    DWORD mob_vnum = pMob->m_table.dwVnum;
                    int iFounded = SECTREE_MANAGER::instance().GetMonsterCountSpawned(mob_vnum);
                    std::string nume_item(Titem->szLocaleName);
                    boost::algorithm::replace_all(nume_item, " ", "_");
                    std::string nume_mob(name_monster.c_str());
                    boost::algorithm::replace_all(nume_mob, " ", "_");
                    ch->ChatPacket(CHAT_TYPE_COMMAND, "searched_item %d %s %s %d %d %d %d %d", iIndex, nume_item.c_str(), nume_mob.c_str(), v[i].dwVnum, v[i].iCount, v[i].iProcent, iFounded, mob_vnum);
                    iIndex = iIndex + 1;
                    iGasite = iGasite + 1;

                }

            }
        }
    }

    ch->ChatPacket(CHAT_TYPE_COMMAND, "searched_item_count %d", iGasite);

    iGasite = iGasite = NULL;
    iIndex = iIndex = NULL;
}

Compiler error:

item_manager.cpp:1136:22: error: assigning to 'DWORD' (aka 'unsigned int') from incompatible type 
  'nullptr_t'
    iGasite = iGasite = NULL;
                        ^~~~
/usr/include/sys/_null.h:35:14: note: expanded from macro 'NULL'
#define NULL    nullptr
            ^~~~~~~
item_manager.cpp:1137:20: error: assigning to 'int' from incompatible type 'nullptr_t'
    iIndex = iIndex = NULL;
                      ^~~~
/usr/include/sys/_null.h:35:14: note: expanded from macro 'NULL'
#define NULL    nullptr
            ^~~~~~~
2 errors generated.

Solution

  • You are using NULL as a constant for zero, when it has been defined as:

    // in /usr/include/sys/_null.h:35:14
    #define NULL nullptr
    

    Since C++11, this is the standard definition.

    The type of nullptr is std::nullptr_t, not an integer, so it can't be assigned to DWORD iGasite which is an unsigned int.

    Simply remove all places where NULL has been used and replace it with either 0 or nullptr appropriately.