Pergunta

I have two classes, one inherited from the other. When I compile, I get the following errors:

Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base@Parsables@Utility@@QAE@XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Entity@Parsables@Utility@@QAE@XZ)

Entity.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Utility::Parsables::Base::~Base(void)" (??1Base@Parsables@Utility@@UAE@XZ) referenced in function "public: virtual __thiscall Utility::Parsables::Entity::~Entity(void)" (??1Entity@Parsables@Utility@@UAE@XZ)

D:\Programming\Projects\Caffeine\Debug\Caffeine.exe : fatal error LNK1120: 2 unresolved externals

I really can't figure out what's going on.. can anyone see what I'm doing wrong? I'm using Visual C++ Express 2008. Here are the files..

"include/Utility/Parsables/Base.hpp"

#ifndef CAFFEINE_UTILITY_PARSABLES_BASE_HPP
#define CAFFEINE_UTILITY_PARSABLES_BASE_HPP

namespace Utility
{
 namespace Parsables
 {
  class Base
  {
  public:
   Base( void );
   virtual ~Base( void );
  };
 }
}

#endif //CAFFEINE_UTILITY_PARSABLES_BASE_HPP

"src/Utility/Parsables/Base.cpp"

#include "Utility/Parsables/Base.hpp"

namespace Utility
{
 namespace Parsables
 {
  Base::Base( void )
  {
  }

  Base::~Base( void )
  {
  }
 }
}

"include/Utility/Parsables/Entity.hpp"

#ifndef CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP
#define CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP

#include "Utility/Parsables/Base.hpp"

namespace Utility
{
 namespace Parsables
 {
  class Entity : public Base
  {
  public:
   Entity( void );
   virtual ~Entity( void );
  };
 }
}

#endif //CAFFEINE_UTILITY_PARSABLES_ENTITY_HPP

"src/Utility/Parsables/Entity.cpp"

#include "Utility/Parsables/Entity.hpp"

namespace Utility
{
 namespace Parsables
 {
  Entity::Entity( void )
  {
  }

  Entity::~Entity( void )
  {
  }
 }
}
Foi útil?

Solução

The relevant bit is this:

unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)"

You need to provide a definition for Base::Base and Base::~Base. A declaration is not good enough. Even if you have nothing to do in either function, you need to leave an empty function body, because C++ actually requires the function to exist. C++ puts things like virtual table maintenance inside your constructors and destructors, so they must be defined even if you don't need to do anything there -- C++ has to do things in there.

Are you sure Base.cpp is being included in the build?

Outras dicas

Just encountered this exact same error today in Visual Studio 2015. Unfortunately the accepted answer didn't worked (as well as answers from many same questions). The thing that worked for me was right click on the base class cpp file, exclude and then include it again. I think somehow VS got confused while moving file around and renames and it just silently refused to compile it even though it was marked as "Included In project" = true in property editor as well as listed in vcproj file in group. This is horrible error and ended up spending good hour on it.

Either your base.cpp is not being compiled/linked or you have a misspelling in it

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top