My static Class Member in my plugin got LNK2005 Error : symbol already defined in object when I was trying to update my project to UE 5.5, and it can work correctly in UE 5.2
source codes are below:
These code comes from the plugin:
SoulConnect.h
// All Right Reserved
#pragma once
#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "SoulPerkTree.h"
#include "SoulConnect.generated.h"
USTRUCT(BlueprintType)
struct FSoulConnection {
GENERATED_BODY()
public:
...
static double ConnectionWeight;
};
double FSoulConnection::ConnectionWeight = 0.005f;
SoulConnect.cpp
// All Right Reserved
#include "SoulConnect.h"
FPrimaryAssetId USoulConnect::GetPrimaryAssetId() const
{
static const FPrimaryAssetType AssetType = TEXT("SoulConnect");
return FPrimaryAssetId(AssetType, GetFName());
}
And PerkTreeActorComponent.h used that header in include, and it(PerkTreeActorComponen) is in my project UE511SV2K
Current Problem is:
FSoulConnection::ConnectionWeight has already defined
in Module.UE511SV2K.cpp.obj
(Raised by PerkTreeActorComponent.cpp.obj / USoulTreeRenderUMG.cpp.obj)
and in Module.SoulTreeDisplayer.cpp.obj
(Raised by SoulConnect.cpp.obj / SoulTreeDisplayer.cpp.obj / SoulTreeEditor.cpp.obj / SoulTreeRenderWidget.cpp.obj)
I’ve tried some ways to solve this issue:
- Delete the implement of ConnectionWeight:
Got error LNK2001 unresolved external symbol (from SoulTreeRenderWidget.cpp.obj / PerkTreeActorComponent.cpp.obj) - Move the implement of ConnectionWeight into cpp file (cut and paste)
Got error LNK2001 unresolved external symbol (from PerkTreeActorComponent.cpp.obj) - Use
double FSoulConnection::ConnectionWeight;
in .h andFSoulConnection::ConnectionWeight = 0.005f;
in .cpp
Got C4430 missing type specifier in SoulConnect.cpp whereFSoulConnection::ConnectionWeight = 0.005f;
located
and C2371 redefinition in SoulConnect.h wheredouble FSoulConnection::ConnectionWeight;
located.
Why should this happen? Can anybody help?