unresolved external symbol in class - c++

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

Related

Why does declaring a struct inside another header file cause a LNK2001 error? [duplicate]

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

Unresolved external symbol "protected static struct" error despite being defined in a .cpp file

This is my eventhandler.h
#pragma once
#include <queue>
#include <Windows.h>
class EventHandler
{
public:
EventHandler()
{
}
~EventHandler()
{
}
static std::queue<MSG*> Events;
};
I've searched a lot to try and solve my problem and all the answers say to declare the static variable in a c++ file, which I've done
#include "EventHandler.h"
std::queue<MSG*> EventHandler::Events;
but I still get
Error LNK2001 unresolved external symbol "protected: static struct tagMSG * Entity::msg" (?msg#Entity##1PAUtagMSG##A)
and I can't figure out why. Have I missed something?
You also need to place your static in cpp file:
// EventHandler.cpp
std::queue<MSG*> EventHandler::Events;

Unresolved external symbol when using static

I am new to c++. I started now playing with classes and I am having a noob problem with statics.
class Test
{
public:
Test(){};
~Test(){};
static void test();
static Helper* helper;
};
void Test::test()
{
Object obj = Test::helper->getObject();
//...
}
When I try to compile it gives the error:
main.obj : error LNK2001: unresolved external symbol "public: static class Helper* Test::helper" (?helper#Test##2PAVHelper##A)
What is wrong with my code?
The first answer is correct. The reason behind this is that you need to allocate memory for static objects outside the class definition. If you define the the class in a header file, and include it in several cpp files, the compiler doesn't know where and how you want to create the object that 'helper' points to.
you need to define Test::helper. Write something like this outside the class:
Helper* Test::helper = new Helper;

C++ error LNK2001 issue

I am fairly new to cpp but have been in c# for a while. I am trying to run a simple console application but I receive this LNK2001 error message.
I have the main.cpp, and have added another class, Zeus, with files, Zeus.h and Zeus.cpp.
Here is the main.cpp:
#include "Zeus.h"
#include <iostream>
int main()
{
Zeus::tick = 25.0;
using std::cout;
cout << "nothing";
}
Here is the Zeus.h:
static class Zeus
{
public:
static void testing(void);
public:
static double tick;
};
And here is the Zeus.cpp:
void Zeus::testing(void)
{
//Doesnt get this far
//But eventually something like
// cout << "test " << Zeus::tick;
}
And here is the error message:
Error 20 error LNK2001: unresolved external symbol "public: static double Zeus::tick"
Thanks,
You need to define Zeus::tick, typically you would to that in the in the Zeus.cpp file. You have only declared it.
double Zeus::tick = 0.0;
Also, there is no static class in C++.
As an aside, free functions can be put in namespaces, as opposed to being static functions of classes. This is the preferred way in C++, unless there are strong reasons for the function to be static.
namespace Dionysus {
void testing();
}
As the error message says: there's no definition of Zeus::tick. Add this to Zeus.cpp:
double Zeus::tick;
Oh, and in Zeus.h remove the static from
static class Zeus
In the main() function you have, what do you mean by a statement Zeus::tick = 25.0;?
Zeus is a class. So to access the individual elements of it you need to create its instance. Its just like a structure where you first create its instance to access its individual elements.
Try the following:
int main() {
Zeus myobject;
myobject.tick = 25.0;
/* Rest of the definition */
}

Initializing static variables in static function result in unresolved

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.