Overloading default constructor causing error [duplicate] - c++

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

Related

LNK2019 Error with nested class in C++

I am new to C++, so I am sorry if this a simple or obvious error. I have been reading a lot other questions and the documentation, but I haven't been able to find a solution yet.
I am trying to add a new class definition to a large existing project. Everything compiles perfectly fine without my additions. However, when I add my code below I get a LNK2019 error on the constructor method (and any additional method). I have noticed adding/referencing properties does not cause this linker error, only the methods. Below is the most simple example that produces the error:
Header:
namespace foo
{
class bar_interface
{
public:
//My code
#ifndef Point_H
#define Point_H
class Point
{
public:
Point(int x, int y);
};
#endif
//existing code
void onStartup();
}
}
Class:
//My code
#ifndef Point_H
#define Point_H
class foo:bar_interface::Point{
public:
Point(int x, int y)
{
};
};
#endif
//existing code
void bar_interface::onStartup()
{
foo::bar_interface::Point p( (int)8, (int)8 );
//continue existing code
}
Error 1 error LNK2019: unresolved external symbol "public: __thiscall
foo::bar_interface::Point::Point(int,int)"
(??0Point#bar_interface#foo##QAE#HH#Z)
referenced in function "public: void __thiscall
foo::bar_interface::onStartup(void)"
(?onStartup#bar_interface#foo##QAEXXZ)
I realize that probably do not need such explicit calls to Point or casting the numbers as ints, but I wanted to make sure I wasn't missing anything obvious (removing them doesnt change the error). I have tried moving the 'Point' class to its own file and defining it outside the 'bar_interface' but within the 'foo' namespace. Removing the #ifndef code creates a C2011 redefinition error. I am at a loss for how to proceed.
Unresolved external means that the definition is missing, that is that the linker cannot find an implementation of the named function.
Somewhere you need:
namespace foo
{
bar_interface::Point::Point(int,int)
{ ... }
}
Remove all your lines from the code above that start from # and the reason of the issue becomes cleaner.

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.

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.

error LNK2001: unresolved external symbol 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.

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