Why am I getting this error ? “unresolved external

2019-08-31 18:00发布

问题:

This question already has an answer here:

  • What is an undefined reference/unresolved external symbol error and how do I fix it? 33 answers

This is the error I have been getting the whole time and I've been trying to figure out how to fix it but have failed. I am asking if anyone can point me to the right direction.

 WorldServer fatal error LNK1120: 2 unresolved externals
WorldServer error LNK2019: unresolved external symbol "public: class CItemElem * __thiscall CLinkedItemMgr::GetLinkedItem(unsigned long)" (?GetLinkedItem@CLinkedItemMgr@@QAEPAVCItemElem@@K@Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem@CDPSrvr@@AAEXAAVCAr@@KKPAEK@Z)
WorldServer error LNK2019: unresolved external symbol "public: int __thiscall CLinkedItemMgr::AddLinkedItem(class CItemElem *)" (?AddLinkedItem@CLinkedItemMgr@@QAEHPAVCItemElem@@@Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem@CDPSrvr@@AAEXAAVCAr@@KKPAEK@Z)

This is the .h

#ifndef __ITEM_LINK__H
#define __ITEM_LINK__H
class CLinkedItemMgr
{
private:
    CLinkedItemMgr(){ m_dwLinkedItemCount = 0;};
    ~CLinkedItemMgr(){};
    DWORD m_dwLinkedItemCount;
public:
    map<DWORD,CItemElem*> m_mapLinkedItems;

    static CLinkedItemMgr *GetInstance()
    {
        static CLinkedItemMgr instance;
        return &instance;
    }
    int AddLinkedItem(CItemElem *pItem);
    CItemElem *GetLinkedItem(DWORD dwIndex);
};
#endif

this is the .cpp

 #include "stdafx.h"
#include "ItemLink.h"
int CLinkedItemMgr::AddLinkedItem(CItemElem *pItem)
{
    if(!pItem)
        return 0;
    m_mapLinkedItems.insert(make_pair<DWORD,CItemElem*>(++m_dwLinkedItemCount,pItem));
    return m_dwLinkedItemCount;
}
CItemElem *CLinkedItemMgr::GetLinkedItem(DWORD dwIndex)
{
    map<DWORD,CItemElem*>::iterator it = m_mapLinkedItems.find(dwIndex);
    if(it == m_mapLinkedItems.end())
        return FALSE;
    return it->second;
}

回答1:

Your problem is in the cpp here.

#ifdef __ITEM_LINK
#include "ItemLink.h"

#ifdef __ITEM_LINK means "only process the code below if __ITEM_LINK is defined"

And in your case, it is not defined. It only gets defined when "ItemLink.h" is included, and "ItemLink.h" only gets included if it's already defined. You've prevented either from happening first.

Remove the #ifdef line.



回答2:

It looks like a linking problem. The compiler knows your class has a function called GetLinkedItem but can't find any definition of that function anywhere. Are you linking properly when compiling your executable? I bet stopping the compiler before linking doesn't trigger any error. (e.g. g++ -c ItemLink.cpp).

i'm going to ask the help of someone who's accustomed to visual studio to elaborate more :D anyway, compiling requires three major steps: 1) applying preprocessor directives, parsing the source code, looking for syntax errors and the like 2) creating an object file from source code (something half-way between source code and executable) 3) linking all the object files making up your project in one executable

your compiling chain fails at the third step. the compiler expects a certain function to be defined in some .cpp (that has become an object file at step 2 of compiling chain) but can't find it anywhere.

and it can't find it because of that #ifdef in the .cpp file, which tells the preprocessor NOT TO INCLUDE your definitions, since __ITEM_LINK is not defined

i see you changed the .cpp in your question by the way