MSVC++ Linker error which I do not quite understand [duplicate] - c++

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
Right I am getting weird linker errors which I've never seen before and which I can't really decipher.
Code for the RelationOwner and RelationUser can be found there. One remark: I've moved all function bodies to the source file instead of the header file. Naming has stayed the same.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::RelationUser<class Family,class Citizen>(class Family *)" (??0?$RelationUser#VFamily##VCitizen####QAE#PAVFamily###Z) referenced in function "public: __thiscall Citizen::Citizen(class Family &,class Name)" (??0Citizen##QAE#AAVFamily##VName###Z) *path*\Citizen.obj CodeAITesting
Error 2 error LNK2019: unresolved external symbol "public: __thiscall RelationUser<class Family,class Citizen>::~RelationUser<class Family,class Citizen>(void)" (??1?$RelationUser#VFamily##VCitizen####QAE#XZ) referenced in function __unwindfunclet$??0Citizen##QAE#AAVFamily##VName###Z$1 *path*\Citizen.obj CodeAITesting
Error 3 error LNK2019: unresolved external symbol "public: __thiscall RelationOwner<class Family,class Citizen>::~RelationOwner<class Family,class Citizen>(void)" (??1?$RelationOwner#VFamily##VCitizen####QAE#XZ) referenced in function __unwindfunclet$??0Family##QAE#VName###Z$1 *path*\Family.obj CodeAITesting
The first one is about a constructor and the second two are about the destructor. That I understand.
I've also implemented my own version of the User and Owner as the following:
// header of Family.h (Owner in the linked PDF file)
#include "Name.h"
#include "RelationOwner.h"
class Citizen;
class Family; // I didn't really know if this one was necessary.
class Family
: public RelationOwner<Family, Citizen>
{
public:
Family(Name name);
private:
Name name;
};
// Source of Family.cpp
#include "Name.h"
Family::Family(Name name)
: name(name)
{
}
//Source of Citizen.h (User in the linked PDF)
#include "Name.h"
#include "RelationUser.h"
class Citizen;
class Family;
class Citizen
: public RelationUser<Family, Citizen>
{
public:
Citizen(Family &family, Name name);
private:
Name name;
};
// Source of Citizen.cpp
#include "Family.h"
#include "Name.h"
#include "RelationUser.h"
Citizen::Citizen(Family &family, Name name)
: RelationUser<Family, Citizen>(&family),
name(name)
{
}
As far as I know I am not doing anything fancypancy, yet it is complaining big time and I don't know why.

Well as Brian Beuning told me "usually the code for a template is in the header file".
And after reading n.m.'s linked possible duplicate I moved the implementation back to the header file and, it worked.
I guess that the duplicate question was spot on.

Related

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.

How to fix C++ linker error with no explaination

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
}

LNK2019 Error: unresolved external symbol

I am getting an error in Visual Studio when compiling my program.
Error LNK2019 unresolved external symbol "public: __cdecl
Grid::Grid(void)" (??0Grid##QEAA#XZ) referenced in function
main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall
Grid::~Grid(void)" (??1Grid##QAE#XZ) referenced in function
_main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
This project works fine at my university but not on my own computer and I am not sure what is wrong.
My main.cpp:
#include <iostream>
#include "Grid.h"
using namespace std;
int main(int args, char **argv)
{
Grid grid;
// grid.LoadGrid("Grid1.txt");
// grid.SaveGrid("OutGrid.txt");
system("pause");
}
And my header file:
#pragma once
class Grid
{
public:
Grid();
~Grid();
void LoadGrid(const char filename[]);
void SaveGrid(const char filename[]);
private:
int m_grid[9][9];
};
Any help at all is appreciated, thanks.
Issue resolved from advise given on [error LNK2019: unresolved external symbol "public: __thiscall : constructor issue
"First in the library project do rightclick->properties, then under the tab General, Configuration Type should be Static library (.lib)."
Thanks everyone for your answers.
As per my understanding your grid class constructor and destractor implementation are missing. You should check your .cpp file, implemention like this
Grid(){}
~ Grid(){}

Unresolved Externals when using polymorphism

Hi i'm getting the following errors:
Error 9 error LNK1120: 2 unresolved externals
Error 8 error LNK2019: unresolved external symbol "public: virtual __thiscall physics::~physics(void)" (??1physics##UAE#XZ) referenced in function "public: virtual void * __thiscall physics::`scalar deleting destructor'(unsigned int)" (??_Gphysics##UAEPAXI#Z)
Error 7 error LNK2019: unresolved external symbol "public: virtual __thiscall student::~student(void)" (??1student##UAE#XZ) referenced in function __unwindfunclet$??0physics##QAE#XZ$0
which occur using the following code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class student{
protected:
string fName,sName;
int id;
vector<string> cname;
vector<int> cmark;
public:
virtual ~student();
virtual void addCourse(string name, int mark)=0;
};
class physics : public student{
public:
physics(){//Initialize default values
fName="blank";
sName="blank";
id=0;
cname.push_back("none");
cmark.push_back(0);
};
~physics();
void addCourse(string name, int mark){
if(cname.size()==1){//override default value for a size of 1
cname[0]=name;
cmark[0]=mark;
}
else{
cname.push_back(name);
cmark.push_back(mark);
}
}
};
The above compiles fine but when i try to initialize an object in main() by using:
int main(){
//Database Storage
vector<student*> DB;
DB.push_back(new physics);
}
That's when i get the errors (removing the push_back line fixes it but i need this for my program). What am i doing wrong?
Turns out adding braces to the end of the destructors fixed it. What difference does that make? (from the comments)
The difference is that in one case you have a declaration which lacks a definition; in the second case you provide a (empty) definition inline.
Trying to invoke a function that is declared but not defined (as in the first case) result in an unresolved reference error raised by the linker - after all, what should it do when a function invocation is found for a function whose implementation is not available?

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