CPP error LNK2019: unresolved external symbol cpp [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.
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall Sounds::soundBox(void)" (?soundBox#Sounds##QAEXXZ) referenced in function _main
For some reason i get this error, and i rly dont know what i did wrong.
Got wimm.lib added playsound works when called from main()
When i try to call it from class in playsound.cpp it calls error...
playsounds.h
#pragma once
#include <Windows.h>
class Sounds
{
public:
Sounds();
~Sounds();
void soundBox();
};
playsound.cpp
#include "playsound.h"
Sounds::Sounds()
{
}
void soundBox()
{
PlaySound(TEXT("fx/boom1.wav"), NULL, SND_FILENAME);
}
Sounds::~Sounds()
{
}
main.cpp
#include <iostream>
#include <conio.h>
#include "playsound.h"
int main()
{
Sounds newsound;
newsound.soundBox();
_getch();
}

You need to change the function definition in playsound.cpp
void soundBox()
To
void Sounds::soundBox()
This is because the function exists within the scope of the Sounds class, so you have to define it as such. Otherwise it would be a free function, and the version of the function in your Sounds class would be undefined (which is what the error is telling you).

Related

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(){}

The good ol' LNK2001 [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 5 years ago.
I have 3 headers
DirectX.h,Draws.h,Memory.h d
Every function has a definition in its corresponding .cpp so that is ruled out except for DirectX.h ofc
I tried a set of solutions to fix it but without success, like not including stdafx.h in all on the Headers.
A little snippet of the three
Memory.h
#pragma once
#include "stdafx.h"
Class Memory
{
foo..
};
extern Memory *gMemory;
Draws.h
#pragma once
#include "stdafx.h"
Class Drawing
{
foo..
};
extern Drawing *Draw;
DirectX.h
#pragma once
#include "stdafx.h"
struct Direct
{
foo
};
extern Direct *DirectX;
Error
1>dllmain.obj : error LNK2001: unresolved external symbol "class Memory * gMemory" (?gMemory##3PAVMemory##A)
1>dllmain.obj : error LNK2001: unresolved external symbol "class Drawing * Draw" (?Draw##3PAVDrawing##A)
1>dllmain.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX##3PAUDirect##A)
1>Draws.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX##3PAUDirect##A)
stdafx.h
#include <windows.h>
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include "Memory.h"
#include "Draws.h"
#include "DirectX.h"
dllmain.cpp
The only parts where I'm using the extern are
gMemory->FindPattern(..);
if (!DirectX->Line)
{
D3DXCreateLine(pDevice, &DirectX->Line);
}
Draw->Circle(foo);
extern keyword means, that object is defined somewhere else (in other compilation unit, in linked static lib), in simple words, it's link to the objects, that are instantiated (created) somewhere else, but can be used in the current compilation unit.
In you dllmain.cpp you should declare variables in global (for example) scope:
// Declaration of pointers.
Drawing* Draw = NULL;
Direct* DirectX = NULL;
Memory* gMemory = NULL;
and in int dllmain() (or whatever main function you have):
// Initialization of pointers with newly created objects:
Draw = new Drawing();
DirectX = new Direct();
gMemory = new Memory();

C++ Unresolved Externals because of header [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 just started with C++ for some OpenGL applications and wanted to pack some monster functions into a util class so that my code remains clean. This is what I did:
awesomeClass.h :
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome();
};
awesomeClass.cpp :
#include "awesomeClass.h"
void do_something_awesome(){
//...
}
main.cpp :
#include "awesomeClass.h"
int main(int argc, char** argv)
{
AwesomeClass::versuchen();
return 0;
}
Output:
Error 3 error LNK1120: 1 unresolved externals \Visual Studio 2013\Projects\TestEnvironmment\Debug\TestEnvironmment.exe TestEnvironmment
Error 2 error LNK2019: unresolved external symbol "public: static void __cdecl AwesomeClass::do_something_awesome(void)" (?do_something_awesome#AwesomeClass##SAXXZ) referenced in function _SDL_main \Visual Studio 2013\Projects\TestEnvironmment\TestEnvironmment\main.obj TestEnvironmment
What is wrong with that code? I mean it works when I paste everything in one file.
You should write
void AwesomeClass::do_something_awesome(){... }
Instead of
void do_something_awesome(){... }
Otherwise the function you implement does not belong to the class.
You should write this if you want write do_something_awesome in separate cpp file.
void AwesomeClass::do_something_awesome(){
//...
}
Or you can use:
#pragma once
class AwesomeClass
{
public:
static void do_something_awesome(){
//code here
}
};

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

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.

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