65704

Why am I getting this error ? “unresolved external symbol” [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix" dir="ltr" rel="nofollow">What is an undefined reference/unresolved external symbol error and how do I fix it?</a> <span class="question-originals-answer-count"> 30 answers </span> </li> </ul>

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; }

Answer1:

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 <strong>not defined</strong>. 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.

Answer2:

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

Recommend

  • How can realize my own memory viewer by QT
  • Error array used as initializer and i don't know the error
  • Difficult linking ui tree widget and header/cpp files
  • How to display accented chars on the console window, that work for all compiler?
  • Rather then create a object in lua, how let lua directly tall the C++ object to launch a method?
  • Example of error caused by using directive in namespaces
  • Finding distance between points in 3D space using correct optimization algorithm C++
  • LoadStringFromFile into compiled script (so available on system that doesn't have the file)? [d
  • how to use Qt setWindowFilePath
  • How to set Input Mask and QValidator to a QLineEdit at a time in Qt?
  • Passing defined type in main to header in C
  • Qt 5.3 Can't make QCompass (QSensor) work on Windows 8.1
  • Can not instantiate another class from main in C++
  • wondering if I can use stl smart pointers for this
  • Header guards and pragma once [duplicate]
  • lex : How to override YY_BUF_SIZE
  • cygwin_exception::open_stackdumpfile: Dumping stack trace to *.exe.stackdump
  • Using header files in C [closed]
  • Can Xcode tell me if I forget to include a category implementation in my target?
  • What is an undefined reference/unresolved external symbol error and how do I fix it?
  • Print text of a QWebview, QPrintDialog doesn't appear
  • compilation error in killable thread class
  • error C2244 on templated member variable with templated argument function, only happens on Visual St
  • How do I add --whole-archive linker option in scons?
  • good resource for socket errors?
  • ReplayKit returns error “RPRecordingErrorFailedToStart”
  • Swift class that inherits an Objective-C Base Class not casting to correct type
  • QML cannot assign assign shared_ptr to [unknown property type]
  • Exposing an enum in a QML signal
  • incomplete type 'struct' error in C
  • C: Incompatible pointer type initializing
  • Sencha Touch 2.0 Controller refs attribute not working?
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • What is the “return” in scheme?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal