linker errors: DLL and inheritance - c++

I am looking for the source of the error since a few hours without success. My project consists of two sub projects. The first one is a dll and the second one is an application (exe).
I simplified my original code, which is part of the dll:
#ifndef blub_base
#define blub_base
#include "SomeInterface.hpp"
#include "Object.hpp"
#include <map>
namespace a
{
namespace b
{
class __declspec(dllexport) CBase : public CSomeInterface
{
protected:
CBase() {}
public:
CBase(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
~CBase() { /* concrete implementation in cpp */ }
bool initialize() { /* concrete implementation in cpp */ }
bool shutdown() { /* concrete implementation in cpp */ }
void foo() { /* concrete implementation in cpp */ }
virtual void blubblub(Object* f_object_p) { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const { /* concrete implementation in cpp */ }
};
}
}
#endif
#ifndef blub
#define blub
#include "Base.hpp"
namespace a
{
namespace b
{
class __declspec(dllexport) CChild : public CBase
{
private:
CChild() {}
public:
CChild(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
/// deconstructor
~CChild() { /* concrete implementation in cpp */ }
void blubblub(Object* f_object_p) override { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const override { /* concrete implementation in cpp */ }
};
}
}
#endif
If I am trying to instantiate a CChild object in my application I get linker errors for all functions of the CChild class:
Error 75 error LNK2001: unresolved external symbol "public: virtual void __thiscall a::b::CChild::blubblub(class a::b::Object *)" (?blubblub#CChild#b#a##UAEXPAVObject#23##Z) application.obj Application
Error 74 error LNK2019: unresolved external symbol "public: virtual __thiscall a::b::CChild::~CChild(void)" (??1CChild#b#a##UAE#XZ) referenced in function "public: virtual void * __thiscall a::b::CChild::`vector deleting destructor'(unsigned int)" (??_ECChild#b#a##UAEPAXI#Z) Application.obj Application
Error 73 error LNK2019: unresolved external symbol "public: __thiscall a::b::CChild::CChild(class a::b::SomeDifferentObject *)" (??0CChild#b#a##QAE#PAVSomeDifferentObject#12##Z) referenced in function _main Application.obj Application
Error 77 error LNK2001: unresolved external symbol "protected: virtual bool __thiscall a::b::CChild::blub1(class Object *,class std::map,class std::allocator > > &)const " (?blub1#CChild#b#a##MBE_NPAVObject#23#AAV?$map#KIU?$less#K#std##V?$allocator#U?$pair#$$CBKI#std###2##std###Z) Application.obj Application
Error 76 error LNK2001: unresolved external symbol "protected: virtual bool __thiscall a::b::CChild::blub2(class Object *,class std::map,class std::allocator > > &)const " (?blub2#CChild#b#a##MBE_NPAVObject#23#AAV?$map#KIU?$less#K#std##V?$allocator#U?$pair#$$CBKI#std###2##std###Z) Application.obj Application
Error 78 error LNK2001: unresolved external symbol "protected: virtual void __thiscall a::b::CChild::blub3(class a::b::Object *)const " (?blub3#CChild#b#a##MBEXPAVObject#23##Z) Application.obj Application
I am using Visual Studio and all cpp files are in the project (checked many times). Each function is implemented e.g.
CChild::CChild(SomeDifferentObject* f_object_p) : CBase(f_object_p)
{
}
It seems that the relevant cpp files are not found?!
Thank you very much for your help!
Kind regards,
Bobby

It is not working because classes are always exported. They need to be exported by dll project and imported by projects that use that dll.
To fix this, add header file, for example ab_dll.h and there:
#ifdef AB_DLL_EXPORT
#define AB_DLL_API __declspec(dllexport)
#else
#define AB_DLL_API __declspec(dllimport)
#endif
Then use that macro in your classes:
class AB_DLL_API CBase : public CSomeInterface
{
//...
};
class AB_DLL_API CChild : public CBase
{
//...
};
Also in your VS dll project add AB_DLL_EXPORT in PreprocessorDefinitions so that the classes are exported. This works that way that if AB_DLL_EXPORT is defined in the project then classes will be exported, otherwise will be imported.

Related

Unresolved external symbol c++ for inheritance and constructor

//Baseclass.h
class Baseclass {
private:
uint8_t index;
public:
Baseclass(uint8_t index);
}
//Baseclass.cpp
#include "Baseclass.h"
Baseclass::Baseclass(uint8_t index) {
index = index;
};
//Subclass.h
#include "Baseclass.h"
class Subclass : public Baseclass {
public:
Subclass();
};
//Subclass.cpp
#include "Subclass.h"
#include "Baseclass.h"
Subclass::Subclass() : Baseclass(0) {};
What am I missing? I kept getting LNK2019 Error
Severity Code Description Project File Line Suppression State
Error
LNK2019 unresolved external symbol "public: __thiscall Baseclass::Baseclass(unsigned char)" (??Baseclass##QAE#E#Z) referenced in function "public: __thiscall Subclass::Subclass(void)" (??Subclass##QAE#XZ)
It couldn't link Baseclass constructor. Are you sure there are no issues with compiling it? If you copy pasted all of the code you lack semicolon at the end of baseclass.

Linker error : error LNK2019: unresolved external symbol

I'm new to C++ and have encountered a problem while running my app. I googled the problem but since most results were with linking libraries I started a new thread.
I have a class CResizableDialog which I'm inheriting from my VtkDialogTest2 dialog class.
VtkDialogTest2.h;
#pragma once
#include "CResizableDialog.h"
#ifdef _WIN32_WCE
#error "CDHtmlDialog is not supported for Windows CE."
#endif
// VtkDialogTest2 dialog
class VtkDialogTest2 : public CResizableDialog
{
DECLARE_DYNCREATE(VtkDialogTest2)
public:
VtkDialogTest2(CWnd* pParent = NULL); // standard constructor
virtual ~VtkDialogTest2();
// Overrides
HRESULT OnButtonOK(IHTMLElement *pElement);
HRESULT OnButtonCancel(IHTMLElement *pElement);
// Dialog Data
enum { IDD = IDD_DIALOG4 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
VtkDialogTest2.cpp
#include "stdafx.h"
#include "Geometry.h"
#include "VtkDialogTest2.h"
IMPLEMENT_DYNCREATE(VtkDialogTest2, CResizableDialog)
VtkDialogTest2::VtkDialogTest2(CWnd* pParent /*=NULL*/)
: CResizableDialog(VtkDialogTest2::IDD, pParent),
{
}
VtkDialogTest2::~VtkDialogTest2()
{
}
void VtkDialogTest2::DoDataExchange(CDataExchange* pDX)
{
CResizableDialog::DoDataExchange(pDX);
}
BOOL VtkDialogTest2::OnInitDialog()
{
CResizableDialog::OnInitDialog();
//some code
return TRUE; // return TRUE unless you set the focus to a control
}
BEGIN_MESSAGE_MAP(VtkDialogTest2, CResizableDialog)
ON_BN_CLICKED(IDOK, &VtkDialogTest2::OnBnClickedOk)
END_MESSAGE_MAP()
//some code
I can't figure out what I'm doing wrong. I downloaded an example from the web which uses the CResizableDialog.h class the exact same way and copied both CResizableDialog.h and CResizableDialog.cpp into my project.
The errors I'm getting are;
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "public: __thiscall CResizableDialog::CResizableDialog(unsigned int,class CWnd *)" (??0CResizableDialog##QAE#IPAVCWnd###Z) referenced in function "public: __thiscall VtkDialogTest2::VtkDialogTest2(class CWnd *)" (??0VtkDialogTest2##QAE#PAVCWnd###Z)
1>VtkDialogTest2.obj : error LNK2019: unresolved external symbol "protected: virtual int __thiscall CResizableDialog::OnInitDialog(void)" (?OnInitDialog#CResizableDialog##MAEHXZ) referenced in function "protected: virtual int __thiscall VtkDialogTest2::OnInitDialog(void)" (?OnInitDialog#VtkDialogTest2##MAEHXZ)
1>VtkDialogTest2.obj : error LNK2001: unresolved external symbol "protected: static struct AFX_MSGMAP const * __stdcall CResizableDialog::GetThisMessageMap(void)" (?GetThisMessageMap#CResizableDialog##KGPBUAFX_MSGMAP##XZ)
1>C:\Users\Geometry.exe : fatal error LNK1120: 3 unresolved externals
Any input will be highly appreciated.
The error was because I copied the CResizableDialog.h and CResizableDialog.cpp files directly into the project folder. I later noticed that they didn't show up in the solution window and copied them to the window as well. After that the errors disappeared.

Template Specialization with External Errors

When I attempted to specialize one of my template functions, Visual Studio threw me an external error, including an error for a function that was not specialized.
The three errors:
1>------ Build started: Project: Project3, Configuration: Debug Win32 ------
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::reverse(void)" (?reverse#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::print(void)" (?print#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::insertionSort(void)" (?insertionSort#?$linearList#VFriendToken####UAEXXZ)
Here is the relevant part of the code:
template<class T>
class arrayList : public linearList<T>
{
public:
//other methods
void reverse();
void print();
void insertionSort();
};
template<class T>
void arrayList<T>::reverse()
{
//method body
}
template<>
void arrayList<FriendToken>::insertionSort()
{
//method body
}
template<>
void arrayList<FriendToken>::print()
{
//method body
}
template<class T>
void arrayList<T>::insertionSort(){}
template<class T>
void arrayList<T>::print(){}
Your example shows specializations of the arrayList member functions which I assume are supposed to be overriding their virtual equivalants in linearList. The linker is saying it cant find the virtual members in the class linearList which is not included in your example.
virtual void __thiscall linearList<class FriendToken>::reverse(void)
If I add a definition of linearList like this the linker is quiet (on MSVC2010, I also added a empty FriendToken class to make things work).
template<typename T>
class linearList
{
public:
virtual void reverse() = 0; //pure virtual
virtual void print() = 0;
virtual void insertionSort() = 0;
};
If this is not your problem please post the code for linearList and I will update my answer as that is surely the source of your problem.
If needed for reference here is how I used the function reverse to test:
arrayList<FriendToken> a;
static_cast<linearList<FriendToken>&>(a).reverse();

error LNK2019: unresolved external symbol

I've recently started to program in C++ again, and for the purposes of education, I am working on creating a poker game. The weird part is, I keep getting the following error:
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::Poker(void)" (??0Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic initializer for 'pokerGame''(void)" (??__EpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::~Poker(void)" (??1Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'pokerGame''(void)" (??__FpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: void __thiscall PokerGame::Poker::begin(void)" (?begin#Poker#PokerGame##QAEXXZ) referenced in function _wmain
1>C:\Visual Studio 2012\Projects\LearningLanguage01\Debug\LearningLanguage01.exe : fatal error LNK1120: 3 unresolved externals
I have done some research on the issue, and most point to the constructor and destructor definition in the header and .cpp not matching. I don't see any issues with the header and .cpp.
Here is the code for poker.h:
#pragma once
#include "Deck.h"
using namespace CardDeck;
namespace PokerGame
{
const int MAX_HAND_SIZE = 5;
struct HAND
{
public:
CARD cards[MAX_HAND_SIZE];
};
class Poker
{
public:
Poker(void);
~Poker(void);
HAND drawHand(int gameMode);
void begin();
};
}
And the code in the .cpp:
#include "stdafx.h"
#include "Poker.h"
using namespace PokerGame;
const int TEXAS_HOLDEM = 0;
const int FIVE_CARD = 1;
class Poker
{
private:
Deck deck;
Poker::Poker()
{
deck = Deck();
}
Poker::~Poker()
{
}
void Poker::begin()
{
deck.shuffle();
}
//Draws a hand of cards and returns it to the player
HAND Poker::drawHand(int gameMode)
{
HAND hand;
if(gameMode == TEXAS_HOLDEM)
{
for(int i = 0; i < sizeof(hand.cards); i++)
{
hand.cards[i] = deck.drawCard();
}
}
return hand;
}
};
Because of the comment below, I've rewritten what I had before.
The problem that the linker is complaining about is that you've declared your member functions in Poker, but haven't defined them. How is this? For starters, you're creating a new class and defining separate member functions in it.
Your header file Poker class exists in the PokerGame namespace and your cpp file Poker class exists in the global namespace. To fix that issue, put them in the same namespace:
//cpp file
namespace PokerGame {
class Poker {
...
};
}
Now that they're in the same namespace, you have another issue. You're defining your member functions inside the class body, but not the first one. The definitions simply can't go in the body of a class named the same way. Get rid of the whole class in the cpp file:
//cpp file
namespace PokerGame {
Poker::Poker() {
deck = Deck(); //consider a member initializer instead
}
//other definitions
}
One last thing: you put the private section of your class in the wrong spot. It was in that cpp file class that we just removed. It belongs with the other parts of your class:
//header file
namespace PokerGame {
class Poker {
public:
//public stuff
private:
Deck deck; //moved from cpp file
};
}
Another solution could be: check the cmake file and make sure it (such as in ADD_EXECUTABLE) includes the .cpp file you listed.

Receiving an error based on template class and it's child class's function calls

1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::getfirst(int &)" (?getfirst#?$LinkedSortedList#H##UAE_NAAH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::clear(void)" (?clear#?$LinkedSortedList#H##UAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::print(void)const " (?print#?$LinkedSortedList#H##UBEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::insert(int)" (?insert#?$LinkedSortedList#H##UAE_NH#Z) referenced in function _main
1>main.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::find(int)const " (?find#?$LinkedSortedList#H##UBE_NH#Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall LinkedSortedList<int>::size(void)const " (?size#?$LinkedSortedList#H##UBEHXZ)
1>c:\users\chris\documents\visual studio 2010\Projects\lab0\Debug\lab0.exe : fatal error LNK1120: 6 unresolved externals
This is what I recieve when trying to compile my code. I've narrowed it down to (i believe) this section of code here:
#ifndef _LinkedSortedListClass_
#define _LinkedSortedListClass_
#include "LinkedNode.h"
#include "SortedList.h"
template <class Elm>
class LinkedSortedList: public SortedList<int> {
public:
void clear();
bool insert(Elm newvalue);
bool getfirst(Elm &returnvalue);
void print() const;
bool find(Elm searchvalue) const;
int size() const;
private:
LinkedNode<Elm>* head;
};
#endif
This is the child class of the SortedList, which is this, in case it's needed..
#ifndef _SortedListClass_
#define _SortedListClass_
template <class Elm> class SortedList {
public:
// -------------------------------------------------------------------
// Pure virtual functions -- you must implement each of the following
// functions in your implementation:
// -------------------------------------------------------------------
// Clear the list. Free any dynamic storage.
virtual void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
virtual bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
virtual void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
virtual bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
virtual int size() const = 0;
};
#endif
Thanks so much for any help; our last class taught us nothing of inheritance, but this is project #1 for this class, without being taught inheritance here either, so this is all touch and go for me, despite what I managed to look up on Google.
Your methods aren't defined. So the linker is complaining because it can't link to their definitions.
Maybe it helps if you placed the definitions of your functions in your header file. This makes it easier for the compiler to resolve these external symbols.
I hope this will help.
Regards,
Philinator