Hello everyone ![]()
Here’s a quick solution to the problem mentioned in the title.
I encountered this error:
Example.cpp.obj : error LNK2001: unresolved external symbol "private: static unsigned short AExample::MyNumber"
If you’re wondering why, it’s because I declared the static variable in the header file, but not in the .cpp file. The compiler doesn’t allocate the static variable automatically, so it returns an error. We must allocate it:
Example.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Example.generated.h"
UCLASS()
class EXAMPLE_API AExample : public AActor
{
GENERATED_BODY()
private:
// However, the variable has not been declared. We must do this in the .cpp file.
static uint16 MyNumber;
}
Example.cpp:
#include "Example/Example.h"
// Allocate here with a default value
uint16 AExample::MyNumber = 0;
The problem will be resolved after that.
As an alternative, it is not recommended to do that in Unreal Engine C++, but here’s a hint:
// Declare with a inline
inline static uint16 MyNumber;
But as already said, it is not recomanded to do that, because it’s more power consumer than just declare live the precedent example and you need the cpp 17.
I hope that help you!
If you have some hint or I made mistakes, write above !!!