This question already has answers here:
static variable link error [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to make a text-based console video game in c++.
I have an Inventory class with a header and a source file. In the same header file I have declared a struct called STRINGSIZES with two integer members called name and description. They are used to store the length that should be used when displaying the Items, or from which length the name will be shortened. The header file "Inventory.h" looks something like this:
#pragma once
#include <iostream>
struct STRINGSIZES { //the fixed Size with which Inventory lists will be printed
int name;
int description;
};
class Inventory
{
public:
//some unrelated code
Inventory();
static void setStringSizes(int name, int description);
private:
static STRINGSIZES stringSizes;
};
In the Inventory.cpp file, I define the setStringSizes() method like so:
void Inventory::setStringSizes(int name, int description)
{
stringSizes.name = name;
stringSizes.description = description;
}
In my main Source.cpp file, I am firstly calling this method.
Now when I try to compile this I get the errors:
1>Inventory.obj : error LNK2001: unresolved external symbol "public: static int stringSizeNames::name" (?name#stringSizeNames##2HA)
1>Inventory.obj : error LNK2001: unresolved external symbol "public: static int stringSizeNames::description" (?description#stringSizeNames##2HA)
What is the problem here?
The error message refers to a variable which declaration you did not show in the question. But I think it is the same problem as with the static data member stringSizes.
Within the class definition this data member
static STRINGSIZES stringSizes;
is a declaration that is not a definition.
You have to define it outside the class definition in the cpp file for example like
STRINGSIZES Inventory::stringSizes;
If your compiler supports the C++17 Standard then you could define the data member inside the class definition the following way
class Inventory
{
//...
inline static STRINGSIZES stringSizes = {};
};
I have been trying for hours to get rid of my errors but it won't go away, i have looked up other posts but nothing seems to work.
I got my .H file with something like this:
using namespace std;
class common
{
public:
common();
static double common::s_a;
static double common::s_b;
Then i got a .CPP file where i've defined those variables like this:
#include "common.h"
common::common()
{
common::s_a = 100;
common::s_b = 100;
}
Then i got this error message(actual variable name instead of a)
common.obj : error LNK2001: unresolved external symbol "public: static
double common::s_playerMaxHealth" (?s_playerMaxHealth#common##2NA)
EDIT : The problem is static, if i remove static i don't have the error anymore. However i need to use static for it to work as intented.
You must define these variables like so (in your .cpp file, outside of any function):
double common::s_a;
double common::s_b;
This is a declaration (not a definition):
class common
{
static double common::s_a;
static double common::s_b;
This is a use (not a definition either):
common::common()
{
common::s_a = 100;
common::s_b = 100;
}
This question already has answers here:
Unresolved external symbol on static class members
(6 answers)
Closed 10 years ago.
It is very important that my function is static, I need to access and modify another static/non-static class member in order to print it out later. How can I do that?
Flow
Class is initiated
Constructor sets variable to something using internal function that must be static
Some time later I print that variable
Example code
#include <iostream>
class MyClass
{
public:
static int s;
static void set()
{
MyClass::s = 5;
}
int get()
{
return MyClass::s;
}
MyClass()
{
this->set();
}
};
void main()
{
auto a = new MyClass();
a->set(); // Error
std::cout << a->get() << std::endl; // Error
system("pause");
}
Error
LNK2001: unresolved external symbol "public: static int MyClass::s" (?s#MyClass##2HA)
LNK1120: 1 unresolved externals
You have declared your static variable, but you have not defined it.
Non-static member variables are created and destroyed as the containing object is created and destroyed.
Static members, however, need to be created independently of object creation.
Add this code to create the int MyClass::s:
int MyClass::s;
Addendum:
C++17 adds inline variables, allowing you code to work with a smaller change:
static inline int s; // You can also assign it an initial value here
^^^^^^
class PossibilisticShellClustering
{
public:
PossibilisticShellClustering(void);
~PossibilisticShellClustering(void);
static void SetParameters(double deltaDistance);
static double deltaDistance
};
and i wanto to initialize static variable deltaDistance in function SetParameters. So in *.cpp file I wrote
void PossibilisticShellClustering::SetParameters(double deltaDistance)
{
PossibilisticShellClustering::deltaDistance = deltaDistance;
}
however I get linker erros
unresolved external symbol "public:
static double
PossibilisticShellClustering::deltaDistance"
(?deltaDistance#PossibilisticShellClustering##2NA)
Could somebody tell me why ?
PossibilisticShellClustering.obj
You need to defined PossibilisticShellClustering::deltaDistance in a source file somewhere in your program, usually a .cc or .cpp file.
double PossibilisticShellClustering::deltaDistance;
What you have in the class body (or would have if it was terminated with a ;) is only a declaration. Static data members also need a definition.
Very simply put:
I have a class that consists mostly of static public members, so I can group similar functions together that still have to be called from other classes/functions.
Anyway, I have defined two static unsigned char variables in my class public scope, when I try to modify these values in the same class' constructor, I am getting an "unresolved external symbol" error at compilation.
class test
{
public:
static unsigned char X;
static unsigned char Y;
...
test();
};
test::test()
{
X = 1;
Y = 2;
}
I'm new to C++ so go easy on me. Why can't I do this?
If you are using C++ 17 you can just use the inline specifier (see https://stackoverflow.com/a/11711082/55721)
If using older versions of the C++ standard, you must add the definitions to match your declarations of X and Y
unsigned char test::X;
unsigned char test::Y;
somewhere. You might want to also initialize a static member
unsigned char test::X = 4;
and again, you do that in the definition (usually in a CXX file) not in the declaration (which is often in a .H file)
Static data members declarations in the class declaration are not definition of them.
To define them you should do this in the .CPP file to avoid duplicated symbols.
The only data you can declare and define is integral static constants.
(Values of enums can be used as constant values as well)
You might want to rewrite your code as:
class test {
public:
const static unsigned char X = 1;
const static unsigned char Y = 2;
...
test();
};
test::test() {
}
If you want to have ability to modify you static variables (in other words when it is inappropriate to declare them as const), you can separate you code between .H and .CPP in the following way:
.H :
class test {
public:
static unsigned char X;
static unsigned char Y;
...
test();
};
.CPP :
unsigned char test::X = 1;
unsigned char test::Y = 2;
test::test()
{
// constructor is empty.
// We don't initialize static data member here,
// because static data initialization will happen on every constructor call.
}
in my case, I declared one static variable in .h file, like
//myClass.h
class myClass
{
static int m_nMyVar;
static void myFunc();
}
and in myClass.cpp, I tried to use this m_nMyVar. It got LINK error like:
error LNK2001: unresolved external symbol "public: static class...
The link error related cpp file looks like:
//myClass.cpp
void myClass::myFunc()
{
myClass::m_nMyVar = 123; //I tried to use this m_nMyVar here and got link error
}
So I add below code on the top of myClass.cpp
//myClass.cpp
int myClass::m_nMyVar; //it seems redefine m_nMyVar, but it works well
void myClass::myFunc()
{
myClass::m_nMyVar = 123; //I tried to use this m_nMyVar here and got link error
}
then LNK2001 is gone.
Since this is the first SO thread that seemed to come up for me when searching for "unresolved externals with static const members" in general, I'll leave another hint to solve one problem with unresolved externals here:
For me, the thing that I forgot was to mark my class definition __declspec(dllexport), and when called from another class (outside that class's dll's boundaries), I of course got the my unresolved external error.
Still, easy to forget when you're changing an internal helper class to a one accessible from elsewhere, so if you're working in a dynamically linked project, you might as well check that, too.
When we declare a static variable in a class, it is shared by all the objects of that class. As static variables are initialized only once they are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).
In the below example, static variable counter is a member of the class Demo. Note how it is initialized explicitly outside the class with the initial value = 0.
#include <iostream>
#include <string>
using namespace std;
class Demo{
int var;
static int counter;
public:
Demo(int var):var(var){
cout<<"Counter = "<<counter<<endl;
counter++;
}
};
int Demo::counter = 0; //static variable initialisation
int main()
{
Demo d(2), d1(10),d3(1);
}
Output:
Count = 0
Count = 1
Count = 2
In my case, I was using wrong linking.
It was managed c++ (cli) but with native exporting. I have added to linker -> input -> assembly link resource the dll of the library from which the function is exported. But native c++ linking requires .lib file to "see" implementations in cpp correctly, so for me helped to add the .lib file to linker -> input -> additional dependencies.
[Usually managed code does not use dll export and import, it uses references, but that was unique situation.]