error LNK2001: unresolved external symbol C++ - c++

IN my VC++ code which was compiling fine earlier, I have added a function X() like this:
In the file BaseCollection.h
class Base
{
// code
virtual HRESULT X();
//code
};
IN the file DerivedCollection.h
class Derived:public Base
{
HRESULT X();
}
In the file DerivedCollection.cpp
HRESULT Derived::X
{
// definition of Derived here.
}
Have included the header files also properly in the .cpp file.
But still I don't understand for what reason I am getting the link error:
error LNK2001: unresolved external symbol "public: virtual long
__thiscall Base::X()" (?X#Base##UAEJI#Z)
I am really trying hard to fix this bug. Can anyone kindly help me in getting this issue resolved.
Thanks a lot in advance.

Have you implemented X() in Base? You need to do that, or make it pure virtual:
class Base
{
// code
virtual HRESULT X() = 0; //pure virtual. Base doesn't need to implement it.
//code
};
Also, your definition of X() in Derived looks wrong. You probable need something like this:
HRESULT Derived::X()
{
// definition of Derived here.
}

You're never defining the function X:
HRESULT Base::X()
{
// definition of X
}
You'll also need a definition for Derived::X() since that too is virtual.

Related

Overloading default constructor causing error [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I've been developing C# and Java (among many other languages) for many years, and I'm just now getting my feet wet in C++. I've buried myself in resources on inheritance and classes, but I just can't seem to figure out how to get rid of this pesky error.
My code:
Entity.h:
#ifndef ENTITY_H
#define ENTITY_H
#include <iostream>
class Entity
{
public:
std::string m_name;
void update(float delta);
Entity();
private:
virtual void step(float delta);
};
#endif
Entity.cpp:
#include "Entity.h"
Entity::Entity()
{
m_name = "Generic Entity";
}
void Entity::update(float delta)
{
step(delta);
}
void Entity::step(float delta) {}
Player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include "Entity.h"
class Player : public Entity
{
public:
Player();
private:
virtual void step(float delta);
virtual void draw() const;
};
#endif
Player.cpp:
#include "Player.h"
Player::Player()
{
m_name = "Player";
}
void Player::step(float delta) {}
void Player::draw() const {}
Main.cpp:
int main()
{
return 0;
}
As you can see, I'm not doing anything with the classes, but I'm getting these errors:
Error 3 error LNK1120: 1 unresolved externals C:\[...]\Debug\ConsoleApplication1.exe ConsoleApplication11
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Entity::Entity(void)" (??0Entity##QAE#XZ) referenced in function "public: __thiscall Player::Player(void)" (??0Player##QAE#XZ) C:\[...]\ConsoleApplication1\Player.obj ConsoleApplication1
UPDATE: The code magically works when I comment out the following code in Player.cpp:
/*Player::Player()
{
m_name = "Player";
}*/
It looks like Entity isn't linked to player. Make sure output shows it compiling and that they are both added to your project
You also need to define a virtual destructor in your base class
Edit:
It doesn't have to compile first, my mistake. In C/C++, program creation is in two steps. First the compiler creates obj files for your cpp files, such as Entity.obj, Player.obj and so on. Then the linker will bundle everything together.
In Player.cpp, you say that you will have a class named Entity at some point, so the compiler finds the definition of that class in the .h file. Player.cpp then gets transformed into executable code in Player.obj but it won't contain the Entity.obj executable code. The compilation step works.
Then the linker will try to parse Player.obj and find Entity.obj that the compiler said will exists. If it doesn't then you get the "undefined reference" error, because the definitions found do not match the actual executable.
The virtual destructor is mandatory. The way inheritance works in C++ is with the virtual table. For each class with inheritance, a virtual table (vtable) is created with the virtual entries. When the linking step is performed, the linker will fill the vtable with the actual function. When the code executes, it checks the vtable for that class then execute that function. This allows you to "override" base methods or use the base method if nothing new is added. The virtual destructor creates the entry in that table for the destructor. If there is no entry for a virtual destructor, then the child class will not have an entry and won't be able to destroy the base class properly, which results in undefined behavior

Getting error LNK2019: unresolved external symbol in vS#)!) that compiled fine in VC 6.0

I am trying to compile a 14 year old C++ program with VS2010 C++ compiler (dont ask why :( ). I am getting the following error
Error 10 error LNK2019: unresolved external symbol "public: __thiscall CConfiguration::CConfiguration(void)" (??0CConfiguration##QAE#XZ) referenced in function
"public: __thiscall CWhoisService::CWhoisService(void)" (??0CWhoisService##QAE#XZ)
I have a cpp file CWhoisService.cpp with a header CWhoisService.h
CWhoisService.h:
class CWhoisService
{
public:
HRESULT Initialize(const char * szServiceName, REFCLSID pMetricsCLSID);
CWhoisService();
~CWhoisService();
HRESULT CheckService();
protected:
CConfiguration m_Configuration;
protected:
bool m_bStartedEvenLog;
bool m_bStartedConfiguration;
private:
//Don't want standard constructor to be called
};
CWhoisService.cpp
#include "ConfigurationLib.h"
#include "CWhoisService.h"
CWhoisService::CWhoisService():
m_bStartedEvenLog(false),
m_bStartedConfiguration(false)
{
}
HRESULT CWhoisService::Initialize(const char * szServiceName, REFCLSID pMetricsCLSID)
{
HRESULT hr = S_OK;
//Initialize the configuration library
hr = m_Configuration.Initialize(VERSION_COMPANY,VERSION_SYSTEM);
the ConfigurationLib.h file referenced in the cpp file and included before CWhoisService.h is as follows:
#ifndef _CONFIGURATION_MODULE
#define _CONFIGURATION_MODULE
class CConfigurationBase
{
public:
CConfigurationBase() : m_bInitialized(false) {};
virtual ~CConfigurationBase() {};
virtual HRESULT Initialize(LPCTSTR szCompanyName, LPCTSTR szSystemName, LPCTSTR szGlobalMachineName = NULL) = 0;
virtual bool IsInitialized() { return m_bInitialized;};
protected:
bool m_bInitialized; // True if the object has been initialized
};
class CConfiguration : public CConfigurationBase
{
public:
CConfiguration();
virtual ~CConfiguration();
// Initialized some values for the class. Must be called first!
virtual HRESULT Initialize(LPCTSTR szCompanyName, LPCTSTR szSystemName, LPCTSTR szGlobalMachineName = NULL);
protected:
// This is the function that actually goes about getting values from the registry
// The other Get functions all call this one
virtual HRESULT GetValue(HKEY hkeyBase, LPCTSTR szSectionName, LPCTSTR szValueName, CString * csValue, DWORD * pdwValue, DWORD dwType);
}; // CConfiguration
#endif // _CONFIGURATION_MODULE
everything was fine last time it compiled around 10 years ago. but now it does not seem to find the ConfigurationLib.h file. i made sure it as part of the project. if i removed it from the start of the cpp file I get the error: missing ';' before identifier 'm_Configuration' so ti obviously see it. yet it does not appear to be able to resolve the class.
Any assistance would be appreciated, i have spend last 3 days on this site and many others but no progress.
i have spend last 3 days on this site and many others but no progress
It is always good to understand the errors that are produced by the linker for Visual C++. Then next time you see such an error, it shouldn't take 3 days to figure out. I know the message looks garbled at first, but it really isn't if you know what to look for.
The trick is to choose the parts of the error that makes sense, and skip over the name-mangling (the gobbledy-gook that looks like the linker is swearing at you). Sometimes the name-mangling is useful, but for your error, it isn't important.
Let's go through the error:
unresolved external symbol "public: __thiscall CConfiguration::CConfiguration(void)"
The line above indicates the function implementation that cannot be found by the linker. The function is CConfiguration::CConfiguration(void). In other words, the 0-argument constructor for CConfiguration cannot be located by the linker.
Next part of the error message states:
referenced in function "public: __thiscall CWhoisService::CWhoisService(void)"
This is the function that is attempting to call the CConfiguration constructor. It is the CWhoisService::CWhoisService(void) constructor. You see it here:
class CWhoisService
{
//...
protected:
CConfiguration m_Configuration;
};
You have a member that is a CConfiguration (m_Configuration), so when you instantiate a CWhoIsService, you are also instantiating a CConfiguration object.
The bottom line is that the linker cannot find the implementation to the CConfiguration constructor that takes no arguments.
Either you
did not add the source module to your project that contains the implementation of the CConfiguration constructor to the project, or
The CConfiguration constructor is in a library and you didn't specify the library to link to in your project, or
You just plain old didn't code a CConfiguration constructor that has no arguments, or
some other unknown issue that causes the linker to miss the code that contains the implementation of the constructor.
My guess is more than likely item 1. above.
Also, this has nothing to do with header files. The header file allows a module to be compiled without error. It does not guarantee that the linker will link successfully.
For example, you can have a module that contains calls to functions that do not exist, but the module will compile successfully. However, at link time, if the function called doesn't actually exist, then you will get the error (as you're seeing now).
At least in the code snippets you showed there is no the constructor definition. It is only declared
class CConfiguration : public CConfigurationBase
{
public:
CConfiguration();
//...

Linker can't find function definitions, LNK2001 unresolved external symbol

Here is my simple setup: (i've hidden lots of unneeded information)
//AutoFocusTest.h
#include "camAVTEx.h"
class CAutoFocusTestApp : public CWinApp
{
protected:
camera_t* mCamera;
public:
virtual BOOL InitInstance();
};
//camAVTEx.h
class camera_avtcam_ex_t : public camera_t
{
public:
camera_avtcam_ex_t();
virtual ~camera_avtcam_ex_t();
//member variables
//member function declarations
}
//camAVTEx.cpp
#include "camAVTEx.h"
camera_avtcam_ex_t::camera_avtcam_ex_t()
{
//stuff
}
camera_avtcam_ex_t::~camera_avtcam_ex_t()
{
//stuff
}
//the rest are defined here in my program
//AutoFocusTest.cpp
#include AutoFocusTest.h
BOOL CAutoFocusTestApp::InitInstance()
{
mCamera = new camera_avtcam_ex_t();
}
This setup produces the error:
3>AutoFocusTest.obj : error LNK2001: unresolved external symbol
"public: __cdecl camera_avtcam_ex_t::camera_avtcam_ex_t(void)"
(??0camera_avtcam_ex_t##QEAA#XZ)
From everything I've read on this relatively common problem, I have not linked something causing my camera_avtcam_ex_t function definitions to not be found. However, I can't figure out what I could have missed. I have added all of the include directories and library directories, as well as added the library files to the additional dependencies section.
Can anyone spot anything that I might be missing?
Assuming you have defined the constructor for your camera_avtcam_ex_t, it's declared as private, you can't instantiate it.

C++ Unresolved external symbol (LNK2019)

I've spent the last 2 days searching for and implementing the answers from similar questions into my code with little success. I have an API that is an external .dll (windows) and I have the header file included into my .cpp file to reference the API.
However I have this issue that no matter what I do, I always get an unresolved external symbol that references this line in my .h file. Yes, I have used Google and modified the answers I found into my code, with no success.
Foo.h
Class Foo {
public:
static Foo* Interface_Get(char* dllfilename);
Foo.cpp
// I declare this just underneath the #include "Foo.h" header
Foo *foo = 0;
Inside my main function I declare it as this (along with some other functions that are fine).
//This has already been created and both Header and .dll are in the same directory.
Foo::Interface_Get("bar.dll");
And I get this error
error LNK2019: unresolved external symbol
"public: static class Foo * __cdecl Foo::Interface_Get(char *)"
I've tried everything I know (This is my first .dll creation experience) I have a feeling I am missing something painfully obvious, but for the life of me I cannot see it.
Entire Foo.cpp
#include "Foo.h"
Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error
Foo* foo = 0;
bool Frame()
{
if (foo->Key_Down(DIK_ESCAPE))
return false;
return true;
}
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
foo->Interface_Get("bar.dll");
foo->System_SetState(grSTATE_FRAME, Frame);
foo->System_SetState(grSTATE_WINDOWED, true);
foo->System_SetState(grSTATE_KEYBOARD, true);
foo->System_Initiate();
foo->System_Start();
foo->System_Shutdown();
foo->Inferface_Release();
return 0;
}
This question explains common problems, and in your case it's (probably) a combination of:
(possibly) forgetting to implement the function
forgetting __declspec(dllexport)
forgetting to link against the library
You need to provide function definition for Interface_Get(char* dllfilename); if you haven't done that.
This only redeclares function again, you need to provide function like below format with {}
Foo* Foo::Interface_Get(char* dllfilename); //May not be redeclared outside class error
Foo.cpp
Foo* Foo::Interface_Get(char* dllfilename)
{
//....
return new Foo();
}

Static members and LNK error in C++

I have a class that has a static member, which I want to use in the class constructor, but the code doesn't compile, and I'm left with these errors:
fatal error LNK1120: 1 unresolved externals
error LNK2001: unresolved external symbol "protected: static class Collection A::collection"
Any help will be appreciated.
Thanks.
a.h:
class A
{
protected:
static Collection<A*> collection;
};
a.cpp:
A::A() {
A::collection.push_back(this);
}
You need to add
Collection<A*> A::collection;
to your a.cpp file.
In your .cpp you need to add:
Collection<A*> A::collection;
The .h only declared that there would be a copy somewhere. You need to provide that copy in the .cpp.
alternatively, if you don't want to put that line in a cpp file, you can use a static method which returns a reference to a static instance... i.e.
class A
{
public:
static Collection<A*>& collection()
{
static Collection<A*> singleInstance;
return singleInstance;
}
};