Unresolved external, trying to use static variable c++ [duplicate] - c++

This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Closed 5 years ago.
I have class World in world.h:
class World
{
public:
static Ground* ground;
};
and in another class in a function I try to use the static variable ground like that:
#include "Node.h"
#include "World.h"
void Node::Foo()
{
Ground* ground = World::ground;
}
and also in world.cpp i have:
#include "stdafx.h"
#include "World.h"
static Ground* ground = new Ground(10, 10);
But i get the following errors:
LNK2001 unresolved external symbol "public: static class Ground World::ground" (?ground#World##2PAVGround##A)
LNK1120 1 unresolved externals

static Ground* ground = new Ground(10, 10);
You're missing World:: there, so you're defining a completely unrelated variable that just happens to have the same name. You should have this:
Ground* World::ground = new Ground(10, 10);

Related

I am getting error LNK2001: unresolved external symbol (C++ code) [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 3 years ago.
1) D:\imp\msgList\fun1.cpp
CMLMessage::MLMessageStatus CMLMessage::getInformationBlocks(const TBase::TLocale& fallbackLocale )
{
CDatabaseHelper::setfallBackLocale(fallbackLocale); // setter function
}
2) D:\imp\commonfolder\fun2.cpp
class CDatabaseHelper
{
Public:
static void setfallBackLocale(TBase::TLocale fallbackLocale)
{
mfallbackLocale = fallbackLocale;
}
Private:
static TBase::TLocale mfallbackLocale; // class member
}
Compiler giving Error:
error LNK2001: unresolved external symbol "private: static struct TBase::TLocale NTrafficInformation::CDatabaseHelper::mfallbackLocale" (?mfallbackLocale#CDatabaseHelper#NTrafficInformation##0UTLocale#TBase##A)
Hi experts Do you have any suggestion for this?
In fun2.cpp, you need to initialize that static member with something like:
TBase::TLocale CDatabaseHelper::mfallbackLocale = TBase::TLocale{"C"};
where the right-hand side is any valid expression producing a TLocale. This line should go after the class definition.
See the cppreference page on "static members" for alternative ways to declare/define that static member.

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();

Static word in c++ showing error [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 5 years ago.
Linker error: tried to make basic cpp program but there is a linker error saying:unresolved symbol "private: static int complex::count". When i removed the static words, its working fine.
Please find Program sample on this
you must explicitly define your static variable outside of the class. For example like this:
class complex {
....
int static count;
....
};
// initialization
int complex::count = 0;

CPP error LNK2019: unresolved external symbol cpp [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.
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).

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
}
};