Search code examples
c++visual-c++linker-errorslnk2019

getting error LNK2019 when i use std::auto_ptr


i ahve a class named FiniteStateMachine declared as below

header file : FiniteStateMachine.h

class FiniteStateMachine
{
    public:
        //Constructor
        FiniteStateMachine();

        //Destructor
        ~FiniteStateMachine();
}

source file : FiniteStateMachine.cpp

////////////////////////////////////////////////////////////////////////
//      Constructor
////////////////////////////////////////////////////////////////////////
FiniteStateMachine::FiniteStateMachine()
:m_InitialState("")
,m_CurrentState(NULL)
,m_Running(false)
{
    RegisterBaseTypes();
}

////////////////////////////////////////////////////////////////////////
//      Destructor
////////////////////////////////////////////////////////////////////////
FiniteStateMachine::~FiniteStateMachine()
{
    if (m_Running) Stop();
    Clear();
}

and ihave a heade file named FSM that i collect all class of project in it

FSM.H

class ICORE_API FiniteStateMachine;

ok i compile it and now wanna to use this class in another library.

every thing about linking that library has been done.

in the client class when i use FiniteStateMachine with auto_ptr i get linker error :

#include "FSM.H"

std::auto_ptr<FiniteStateMachine > fsm;

error LNK2019: unresolved external symbol "public: __thiscall IFSM::FiniteStateMachine::~FiniteStateMachine(void)" (??1FiniteStateMachine@IFSM@@QAE@XZ) referenced in function "public: void * __thiscall FSM::FiniteStateMachine::`scalar deleting destructor'(unsigned int)" (??_GFiniteStateMachine@IFSM@@QAEPAXI@Z)

but by declaring such as this

#include FSM.h

FiniteStateMachine* fsm;

every thing is ok and project completely compiled.

now i want to know why this happen? what is wrong here.


Solution

  • The std::auto_ptr<> generates the code to call the FiniteStateMachine's destructor and in your case you don't provide it, because you only provide by giving the forward declaration.