Unresolved external symbol Error on public static vector [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)
Unresolved external symbol on static class members
(6 answers)
Closed 2 years ago.
Error message in question:
LNK2001 unresolved external symbol "public: static class std::vector<struct MakeKey::KeyStruct,class std::allocator<struct MakeKey::KeyStruct> > MakeKey::KeyArray" (?KeyArray#MakeKey##2V?$vector#UKeyStruct#MakeKey##V?$allocator#UKeyStruct#MakeKey###std###std##A)
Hello i'm kind of new to this whole coding thing and apologize if the error is stupid but I am getting a unresolved external symbol error on both of my public static vectors. I have read through multiple posts on stack overflow and can't seem to figure out what I am doing wrong. Anyway here is the minimal code that produces the same errors. Yes it is important that the vectors remain static because multiple other errors appear if it is non-static.
class MakeKey
{
public:
typedef struct KeyStruct {
sf::Image Img;
sf::Texture Tex;
sf::Sprite Sprite;
}NewKey;
static vector <MakeKey::NewKey> KeyArray;
static vector <sf::RenderWindow*> WindowArray;
static void StepWindows()
{
for (int i{ 0 }; i > MakeKey::KeyArray.size(); i++)
{
WindowArray[i]->clear(sf::Color::Transparent);
WindowArray[i]->draw(KeyArray[i].Sprite);
WindowArray[i]->display();
}
}
};
And my main
int main()
{
MakeKey::StepWindows();
}

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.

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;

error LNK2001: unresolved external symbol "private: static class Game Game::game_" [duplicate]

This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 5 years ago.
class Game
{
public:
static Game& GetInstance()
{
return game_;
}
private :
static Game game_;
Game();
};
Выдает ошибку error LNK2001: unresolved external symbol "private: static class Game Game::game_" (?game_#Game##0V1#A)
Не понимаю что не нравится компилятору
Спасибо
Why did you put class constructor inside private section? This could be the cause of your problem. Move Game() to public segment and see what happens.
Also I think the way you are trying to define and return a static value is wrong all together

unresolved external symbol "private: static float ObjectInfo::Rotation [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 7 years ago.
#include <iostream>
class ObjectInfo{
private:
static float Rotation;
public:
//sets object rotation value
void SetR(float a){ static float Rotation = a; }
//print roation value (I think this is where the problem is located)
void PrintR(){ std::cout << Rotation;}
};
int main()
{
ObjectInfo Wall;
//set float var
float Rotation;
//Get user set rotation
std::cin >> Rotation;
//set wall rotation
Wall.SetR(Rotation);
//print wall rotation value
Wall.PrintR();
std::cin >> Rotation;
}
Error 1 error LNK2001: unresolved external symbol "private: static float ObjectInfo::Rotation" (?Rotation#ObjectInfo##0MA)
Error 2 error LNK1120: 1 unresolved externals
This is a protype i made and i have no clue how to resolve the error.
Does anynyone know what could cause this error?
I get the same error if i try returning the value and then couting that value.
Does anyone know an alteriate solution to retreaving the value from the class?
You need to allocate storage for your static member, need
float ObjectInfo::Rotation;
outside your class definition.

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