How to fix C++ linker error with no explaination - c++

I have a C++ project that won't compile, and the following 2 errors are produced:
Error LNK1120 1 unresolved externals
Error LNK2019 unresolved external symbol "public: virtual __cdecl
StartWindow::~StartWindow(void)" (??1StartWindow##UEAA#XZ) referenced
in function "public: void __cdecl StartWindow::`vbase
destructor'(void)" (??_DStartWindow##QEAAXXZ)
StartWindow is a class I have defined, but currently it is never instantiated or included anywhere in the project. Deleting the class allows the project to compile, but if this class is within the project it won't.
I will include the code for the class in case I am missing something:
.CPP File
#include "StartWindow.h"
StartWindow::StartWindow()
{
setImmediateDrawMode(false);
}
void StartWindow::onDraw()
{
clearScreen(WHITE);
EasyGraphics::onDraw();
}
Header File:
#pragma once
#include "EasyGraphics.h"
class StartWindow : public EasyGraphics
{
public:
StartWindow();
~StartWindow();
private:
virtual void onDraw();
};
Thanks.

You're missing the implementation for the destructor for StartWindow. In your implementation file (.cpp file), append:
StartWindow::~StartWindow(){
//if your destructor is non-trivial, include definition here
}

Related

Unresolved Add_Test symbols, even with test fixture class included in the file

All of a sudden, after my tests had been working for hours, I'm getting errors that it can't find my text fixture functions (SetUp/TearDown). I'd had the fixture defined in a separate file, so as a quick fix I moved the class to the test file, but I still get the same problem!
Here's the current file. I didn't just omit the code for brevity, I've tried building it in this form to make sure the problem isn't somehow caused by the code inside the tests. I've left in the includes though.
#include "memory"
#include "../TestUtility/TestUtility.h"
#include "DependencyInjector.h"
#include "gtest/gtest.h"
#include "RecoverableErrorException.h"
namespace Test
{
class DependencyInjectorTest : public testing::Test
{
public:
DependencyInjectorTest();
virtual ~DependencyInjectorTest();
protected:
virtual void SetUp() { Framework::DependencyInjector::Destroy(); };
virtual void TearDown() override;
};
TEST_F ( DependencyInjectorTest, FindEmpty )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Add )
{
// content ommitted
}
TEST_F ( DependencyInjectorTest, Find )
{
// content ommitted
}
} // namespace Test
And here are the errors:
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::DependencyInjectorTest::DependencyInjectorTest(void)" (??0DependencyInjectorTest#Test##QAE#XZ) referenced in function "public: __thiscall Test::DependencyInjectorTest_Add_Test::DependencyInjectorTest_Add_Test(void)" (??0DependencyInjectorTest_Add_Test#Test##QAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Test::DependencyInjectorTest::~DependencyInjectorTest(void)" (??1DependencyInjectorTest#Test##UAE#XZ) referenced in function "public: virtual __thiscall Test::DependencyInjectorTest_Add_Test::~DependencyInjectorTest_Add_Test(void)" (??1DependencyInjectorTest_Add_Test#Test##UAE#XZ)
1>DependencyInjectorUnitTests.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Test::DependencyInjectorTest::TearDown(void)" (?TearDown#DependencyInjectorTest#Test##MAEXXZ)
UPDATE: The errors disappeared at some point. And then a few hours later, just as mysteriously and frustratingly, came back. Even on files I hadn't worked on since it was fine.
You need to define the constructor, destructor and TearDown methods of DependencyInjectorTest. Even if they are virtual, the compiler needs basic methods to initialize the vtable.
https://en.cppreference.com/w/cpp/language/virtual
By declaring them in the class definition, the macros used them implicitly, and they need to be defined. As they're not, linking results in an undefined symbol error.
You can either remove the declarations or define the functions.

UE4 creating a new c++ gamemode class

I am defining a class in a header file. It's inheritence from another class named AGameMode. In the editor i am creating new class from Gamemode and it's auto generates .h and .cpp files. Problem is, i already include the necessary header files still can't see the class i want to inheritence from it. Instead of it throws me error. Let me explain with code:
//ACollectorMan_GameMode.h
#include "CoreMinimal.h"
#include "GameFramework/GameMode.h"
#include "CollectorMan_GameMode.generated.h"
UCLASS()
class MYPROJECT2_API ACollectorMan_GameMode : public AGameMode
{
GENERATED_BODY()
ACollectorMan_GameMode();
}
when i declare the constructer and compile it shows me error. But if i won't declare it, in the editor line 7(class MYPROJECT2_API ACollectorMan_GameMode : public AGameMode) won't be throw error(still will be red).
This is not only for Gamemode parent class. Character or Pawn classes have same problem. When i build VS gives me that error :
CollectorMan_GameMode.cpp.obj : error LNK2019: unresolved external symbol "private: __cdecl ACollectorMan_GameMode::ACollectorMan_GameMode(void)" (??0ACollectorMan_GameMode##AEAA#XZ) referenced in function "public: static void __cdecl ACollectorMan_GameMode::__DefaultConstructor(class FObjectInitializer const &)" (?__DefaultConstructor#ACollectorMan_GameMode##SAXAEBVFObjectInitializer###Z)
CollectorMan_GameMode.gen.cpp.obj : error LNK2001: unresolved external symbol "private: __cdecl ACollectorMan_GameMode::ACollectorMan_GameMode(void)" (??0ACollectorMan_GameMode##AEAA#XZ)
This should be a linker problem however "GameMode.h" file available at "GameFramework/GameMode.h". I can show you a similiar file. But it's works. There is an example:
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyProject2GameMode.generated.h"
UCLASS()
class AMyProject2GameMode : public AGameModeBase
{
GENERATED_BODY()
AMyProject2GameMode();
};
That all i say.
Thanks in advance.

Unresolved external symbol, cannot figure out why

I have two files that are causing me a lot of grief: camAVTEx.h and camAVTEx.cpp. Here is the general setup for the two files:
//.h////////////////////////////////////////////////
/*
#includes to some other files
*/
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
private:
//some members
public:
//some methods
};
void GlobalShutdownVimbaSystem();
//.cpp/////////////////////////////////////////////
#include "StdAfx.h"
#include "camAVTEx.h"
//some other #includes
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
}
//rest of the class' functions
void GlobalShutdownVimbaSystem()
{
//implememtation
}
Then, in a file in a different directory, I do a #include to the exact location of the .h file and try to use the class:
//otherfile.cpp
#include "..\..\src\HardSupport\Camera.h"
//this is the base camera class (camera_t)
#include "..\..\src\HardControl\camAVTEx.h"
//this is indeed where both the .h and .cpp files are located
void InitCam
{
camera_t* maincam = new camera_avtcam_ex_t();
}
void OnExit()
{
GlobalShutdownVimbaSystem();
}
When I compile, I get the following errors:
8>otherfile.obj : error LNK2001: unresolved external symbol "public: __cdecl
camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ)
8>otherfile.obj : error LNK2001: unresolved external symbol "void __cdecl
GlobalShutdownVimbaSystem(void)" (?GlobalShutdownVimbaSystem##YAXXZ)
8>....\bin\x64\Release\otherfile.exe : fatal error LNK1120: 2 unresolved externals
I cannot for the life of me figure out why it can't find the implementations for these two functions.
So I guess my question is fairly obvious: Why am I getting these errors and what do I need to change to fix them?
Whatever how you look at it, the error you have : unresolved external symbol "public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)" (??0camera_avtcam_ex_t##QEAA#XZ) means that the compiler knows the symbol camera_avtcam_ex_t::camera_avtcam_ex_ (that's the class constructor) since he saw its declaration in the camAVTEx.h file but halas, it can't find (= resolve) the implementation of this symbol (in short, the code).
This usually happen because of several possible causes :
you didn't tell the compiler about the code (.cpp) you try to use so he doesn't know it. Try to add the file to your project.
you compile the missing code, but don't link with it. Check if you don't have two separated projects or try to add the lib to your project if it comes from a lib.
in some way, the compiled code does not match its definition (happens when mixing C and C++ or messing with namespaces) Check if you are not declaring contradicting enclosing namespaces.
(maybe other reasons I don't know ?)

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.
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.

C++ LNK2019 error with constructors and destructors in derived classes

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 )
{
}
}
}
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?
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