std::unordered_map and linker errors - c++

I'm have some problems with compiling my new project.
Compiler outputs:
1>------ Build started: Project: IRPCore, Configuration: Debug Win32 ------
1> core.cpp
1> Creating library D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.lib and object D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.exp
1>core.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache#core##2V?$unordered_map#HUitem##V?$hash#H#std##U?$equal_to#H#3#V?$allocator#U?$pair#$$CBHUitem###std###3##tr1#std##A)
1>main.obj : error LNK2001: unresolved external symbol "public: static class std::tr1::unordered_map<int,struct item,class std::hash<int>,struct std::equal_to<int>,class std::allocator<struct std::pair<int const ,struct item> > > core::itemCache" (?itemCache#core##2V?$unordered_map#HUitem##V?$hash#H#std##U?$equal_to#H#3#V?$allocator#U?$pair#$$CBHUitem###std###3##tr1#std##A)
1>D:\Repo\Inter Role Play Gamemode\sa-mp-0.2-plugin-sdk\IRP\Debug\IRPCore.dll : fatal error LNK1120: 1 unresolved externals
Here comes core.cpp
#include <iostream>
#include <unordered_map>
#include "core.h"
core::core(void)
{
}
core::~core(void)
{
}
void core::Item_Insert(int uid, item Item)
{
core::itemCache.insert(std::make_pair<int,item>(uid, Item));
return;
}
std::string core::convertNativeStringToString(AMX *amx, cell input)
{
char *string = NULL;
amx_StrParam(amx, input, string);
return string ? string : "";
}
Should I link some more libs or so?
Thanks in advance.
Edit: Here comes core definition from core.h
class core
{
public:
static std::unordered_map<int, item> itemCache;
core(void);
~core(void);
static void Item_Insert(int uid, item Item);
static std::string convertNativeStringToString(AMX *amx, cell input);
private:
};

Add a definition of core::itemCache to core.cpp. In general, static data members of classes are declared in the class definition and defined in a source file.

As #Pete Becker is saying, put this at the top of the cpp:
std::unordered_map<int, item> core::itemCache;
in addition to the
static std::unordered_map<int, item> itemCache;
already in the .h.

Related

unresolved external symbol "__declspec(dllimport)"

I'm trying to code a little plugin for bakkesmod because I'm pissing myself off.
I watched the only 2 video that exists on this topic but ... it doesn't work and I have this error for each void - >> Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "__declspec(dllimport) public: void __thiscall GameWrapper::HookEvent(class std::basic_string<char,struct std::char_traits,class std::allocator >,class std::function<void __cdecl(class std::basic_string<char,struct std::char_traits,class std::allocator >)>)" (_imp?HookEvent#GameWrapper##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$function#$$A6AXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z#3##Z) TagName C:\Users\leodu\source\repos\TagName\TagName\TrollTagName.obj 1
here is my code.
TroolTagName.cpp (not judge the name)
#include "TrollTagName.h"
BAKKESMOD_PLUGIN(TroolTagName, "Trool Tag Name", "1.0", PERMISSION_ALL)
void TroolTagName::onLoad()
{
this->Log("This is my first Bakkesmod Plugin");
this->LoadHooks();
}
void TroolTagName::onUnload()
{
}
void TroolTagName::LoadHooks()
{
gameWrapper->HookEvent("Function TAGame.GameEvent_Soccar_TA.EventMatchEnded", std::bind(&TroolTagName::GameEndedEvent, this, std::placeholders::_1));
gameWrapper->HookEvent("Function TAGame.AchievementManager_TA.HandleMatchEnded", std::bind(&TroolTagName::GameEndedEvent, this, std::placeholders::_1));
}
void TroolTagName::GameEndedEvent(std::string name)
{
cvarManager->executeCommand("load_freeplay");
}
void TroolTagName::Log(std::string msg)
{
cvarManager->log("TroolTagName: " + msg);
}
TroolTagName.h
#include "bakkesmod\plugin\bakkesmodplugin.h"
#pragma comment(lib, "pluginsdk.lib")
class TroolTagName : public BakkesMod::Plugin::BakkesModPlugin
{
public:
virtual void onLoad();
virtual void onUnload();
void LoadHooks();
void GameEndedEvent(std::string name);
private:
void Log(std::string msg);
};
The project and a Dynamic-library dll project.
I tried adding __declspec (dllexport) before void but ... I got this error - >> redefinition; different linkage and I found nothing for this error so I am blocked :(

Linker Error "unresolved external symbol" when using constructor of other class

I've been struggling to find why my linker gets an unresolved external symbol error. The error looks like this:
Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader##QAE#PBD#Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##I#Z)
DV2556_Project
grid_world_GPGPU.obj
1
As far as I understand it it has something to do with that my linker finds the declaration of the Shader::Shader(char const *)-function but cannot find the definition. I have been staring at this for hours and cannot figure out why the linker becomes sad.
grid_world_GPGPU.h:
#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H
#include "grid_world.h"
#include <../swift_open_gl.h>
class GridWorldGPGPU : public GridWorld {
private:
Shader* compute_shader_ = nullptr;
public:
GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};
#endif // !GRID_WORLD_GPGPU_H
grid_world_GPGPU.cpp:
GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
: GridWorld(in_side) {
this->compute_shader_ = new Shader(in_path_shader.c_str());
}
The Shader-class is defined in the swift_open_gl.h file:
#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Shader {
public:
Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H
And swift_open_gl.cpp has this:
#include "..\swift_open_gl.h"
inline Shader::Shader(const char * cs_path) {
//Do stuff
}
I've tried with and without the inline (Visual Studio added it when I tried moving the function definition between the .h-file and a .cpp-file, so I decided to try it) and i have verified that the #include <../swift_open_gl.h> doesn't occur anywhere else in the project than the files listed above.
An extra set of eyes to look over this would be appreciated!
Provide default constructor of the class as well.
class Shader {
public:
Shader(){} // default constructor
Shader(const char* cs_path);
};
Dont use inline in Shader::Shader(const char* cs_path){} definition

"unresolved external symbol" error when trying to compile C++ project [duplicate]

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Closed 7 years ago.
I am trying to implement a few classes but i got the following error when compiling the codes. I have tried to removed all the redundant codes but none of them helps. I have no idea what went wrong.
Here is the error i get when compile the code:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: __thiscall FullArray<int>::FullArray<int>(unsigned int)" (??0?$FullArray#H##QAE#I#Z) referenced in function _wmain FinancialDerivatives C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\FinancialDerivatives\FinancialDerivatives.obj 1
Error LNK1120 2 unresolved externals FinancialDerivatives C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\Debug\FinancialDerivatives.exe 1
Error LNK2019 unresolved external symbol "public: virtual __thiscall FullArray<int>::~FullArray<int>(void)" (??1?$FullArray#H##UAE#XZ) referenced in function _wmain FinancialDerivatives C:\Users\Jeremy Nguyen\Documents\Visual Studio 2015\Projects\FinancialDerivatives\FinancialDerivatives\FinancialDerivatives.obj 1
FullArray.h file:
#pragma once
#include <vector>
template <class V>
class FullArray
{
private:
std::vector<V> m_vector; // Use STL vector class for storage
public:
// Constructors & destructor
FullArray();
FullArray(size_t size);
FullArray(const FullArray<V>& source);
FullArray<V>& operator = (const FullArray<V>& source);
virtual ~FullArray();
};
FullArray.cpp file:
#include "stdafx.h"
#include "FullArray.h"
template<class V>
FullArray<V>::FullArray()
{
m_vector = std::vector<V>(1); // vector object with 1 element
}
template<class V>
FullArray<V>::FullArray(size_t size)
{
m_vector = std::vector<V>(size);
}
template<class V>
FullArray<V>::FullArray(const FullArray<V>& source)
{
m_vector = source.m_vector;
}
template<class V>
FullArray<V>& FullArray<V>::operator=(const FullArray<V>& source)
{
// Exit if same object
if (this == &source) return *this;
// Call base class constructor
//ArrayStructure<V>::operator = (source);
// Copy the embedded vector
m_vector = source.m_vector;
return *this;
}
template<class V>
FullArray<V>::~FullArray()
{
}
main file:
#include "stdafx.h"
#include "FullArray.h"
int _tmain(int argc, _TCHAR* argv[])
{
FullArray<int> tmp(5);
return 0;
}
If you compile templates, the source needs to be available at any place where they are used, so include your .cpp file at the bottom of the .h file, or include the .cpp file instead of the .h file.

Templated class linking error when used to declare object in other class

I created a templated data class (CAnyData, please see its header file copy for your reference), with which I declared some variables in my another class (CConstantDataBlock, please see its header file copy for your reference). As you may see, the latter one is nearly an empty class. But when I compiled my project, the VS2008 compiler thowed the following linking errors. Would please help me figure out what's wrong with my CConstantDataBlock and/or CAnyData?
1>------ Build started: Project: Tips, Configuration: Debug Win32 ------
1>Compiling...
1>ConstantDataBlock.cpp
1>Linking...
1> Creating library F:\Tips\Debug\Tips.lib and object F:\Tips\Debug\Tips.exp
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<double>::~CAnyData<double>(void)" (??1?$CAnyData#N##QAE#XZ) referenced in function __unwindfunclet$??0CConstantDataBlock##QAE#XZ$0
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<int>::~CAnyData<int>(void)" (??1?$CAnyData#H##QAE#XZ) referenced in function __unwindfunclet$??0CConstantDataBlock##QAE#XZ$0
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<double>::CAnyData<double>(void)" (??0?$CAnyData#N##QAE#XZ) referenced in function "public: __thiscall CConstantDataBlock::CConstantDataBlock(void)" (??0CConstantDataBlock##QAE#XZ)
1>ConstantDataBlock.obj : error LNK2019: unresolved external symbol "public: __thiscall CAnyData<int>::CAnyData<int>(void)" (??0?$CAnyData#H##QAE#XZ) referenced in function "public: __thiscall CConstantDataBlock::CConstantDataBlock(void)" (??0CConstantDataBlock##QAE#XZ)
1>F:\Tips\Debug\Tips.exe : fatal error LNK1120: 4 unresolved externals
1>Build log was saved at "file://f:\Tips\Tips\Debug\BuildLog.htm"
1>Tips - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#pragma once
#include <string>
using namespace std;
template <class T>
class CAnyData
{
public:
CAnyData(void);
CAnyData(int nWordNumber, string sContents, T Type, int nWidth, int nPrecision);
~CAnyData(void);
// Operators
CAnyData( const CAnyData& rhs );
const CAnyData& operator = (const CAnyData& rhs);
// Must define less than relative to name objects.
bool operator<( const CAnyData& AnyData ) const;
// Compares profile's of two objects which represent CAnyData
inline bool operator ==(const CAnyData& rhs) const;
// Get properties
inline int WordNumber() const { return m_nWordNumber; }
inline const string& Contents() const { return m_sContents; }
inline const T& DataType() const { return m_Type; }
inline int Width() const { return m_nWidth; }
inline int Precision() const { return m_nPrecision; }
// Set properties
void WordNumber(int nWordNumber) const { m_nWordNumber = nWordNumber; }
void Contents(string sContents) const { m_sContents = sContents; }
void DataType(T Type) const { m_Type = Type; }
void Width(int nWidth) const { m_nWidth = nWidth; }
void Precision(int nPrecision) const { m_nPrecision = nPrecision; }
protected:
void Init(void);
protected:
int m_nWordNumber;
string m_sContents;
T m_Type;
int m_nWidth;
int m_nPrecision;
};
#pragma once
#include "AnyData.h"
// Constants block
// This block consists of 64 words to be filled with useful constants.
class CConstantDataBlock
{
public:
CConstantDataBlock(void);
~CConstantDataBlock(void);
protected:
CAnyData<int> m_nEarthEquatorialRadius;
CAnyData<int> m_nNominalSatelliteHeight;
CAnyData<double> m_dEarthCircumference;
CAnyData<double> m_dEarthInverseFlattening;
};
It seems that you do not have definitions for several of the methods of CAnyData, including the default constructor and the destructor. When you use these in your CConstantDataBlock-class, the constructor and destructor are required though.
Since CAnyData is a class-template, all definitions should be written directly into the header-file (just as you have done with all the getters and setters).

Define sorting method ( that use std::sort for sorting) of a templated class in dll and calling it from another project

Sorry for my bad English. I have 2 projects. Project 1 is a MFC dll that contains class CMyContainer, class CEmployee. Project 2 is my main project. In project 2, I created an instance of CMyContainer of type CEmployee. Now I want to sort the container but I got an error
"error LNK2019: unresolved external symbol "bool __cdecl MyComparer(class CEmployee *,class CEmployee *)" (?MyComparer##YA_NPAVCEmployee##0#Z) referenced in function "public: void __thiscall CMyContainer<class CEmployee>::sortContainer(void)" (?sortContainer#?$CMyContainer#VCEmployee####QAEXXZ)"
How can I fix this problem?
// file MyContainer.h in project 1
#include <vector>
template <class T>
class _declspec(dllexport) CMyContainer
{
public:
CMyContainer(void);
~CMyContainer(void);
...
void sortContainer();
private:
std::vector<T*> items;
typename std::vector<T*>::iterator it;
};
template <class T> void CMyContainer<T>::sortContainer()
{
typedef bool (*comparer_t)(T*,T*);
comparer_t cmp = &MyComparer;
std::sort(items.begin(), items.end(), cmp);
}
//File Employee.h in project 1
#include "MyContainer.h"
class _declspec(dllexport) CEmployee
{
public:
CEmployee(void);
~CEmployee(void);
void setEmployeeCode(CString);
CString getEmployeeCode();
friend bool MyComparer(CEmployee*, CEmployee*);
private:
CString m_szEmployeeCode;
}
//File Employee.cpp in project 1
void CEmployee::setEmployeeCode(CString val){
m_szEmployeeCode= val;
}
CString CEmployee::getEmployeeCode(){
return m_szEmployeeCode;
}
bool MyComparer(CEmployee*pEmp1, CEmployee*pEmp2)
{
return (pEmp1->getEmployeeCode().Compare(pEmp2->getEmployeeCode())<0);
}
//File main.cpp in project 2
#include <..\Models\MyContainer.h>
#include <..\Models\Employee.h>
...
CMyContainer<CEmployee> *pListEmployee;
... // insert into pListEmployee
// sort pListEmployee
pListEmployee.sortContainer();//-> This command cause error
Try to export MyComparer from the .dll with _declspec(dllexport)