1

I have a static variable declared in a header file and initialized in the corresponding cpp file. What I'm trying to do is accessing that variable from another cpp file. This is the code:

AkOcclusionObstructionService.h

class AkOcclusionObstructionService
{
public:

    static float OcclusionFadeRate;
    ...
}

AkOcclusionObstructionService.cpp

...
float AkOcclusionObstructionService::OcclusionFadeRate = 3.0f;
...

MyBlueprintFunctionLibrary.cpp

#include "MyBlueprintFunctionLibrary.h"
#include "AkOcclusionObstructionService.h"

void UMyBlueprintFunctionLibrary::ChangeWwiseFadeRate(float rate)
{
    AkOcclusionObstructionService::OcclusionFadeRate = rate;
}

When I try to build Visual Studio gives me this error:

"LNK2001 unresolved external symbol "public: static float AkOcclusionObstructionService::OcclusionFadeRate" (?OcclusionFadeRate@AkOcclusionObstructionService@@2MA)".

It looks like MyBlueprintFunctionLibrary can't see the OcclusionFadeRate variable in the AkOcclusionObstructionService cpp file.

Anyone has an idea of what is happening?

Thanks in advance!

EDIT: Additional Linking/building info:

To make the "AkOcclusionObstructionService.h" file visible to the "MyBlueprintFunctionLibrary.cpp" file we had to add something to the "TestFadeRateWwise2.build.cs" (TestFadeRateWwise2 is the project name). Specifically we had to add "AKAUDIO" to this line:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "AKAUDIO" });

That's the only "non standard" thing we did.

  • There's nothing in the code you show that is inherently wrong, so the fault must be in the code you don't show or the way you compile it. – Ulrich Eckhardt Mar 06 '18 at 16:35
  • This is not a code but a build issue. How do you build (or in your case link) your code? – TobiSH Mar 06 '18 at 16:41

2 Answers2

1

in the cpp file you should do like this.

//.cpp
#include "MyBlueprintFunctionLibrary.h"
#include "AkOcclusionObstructionService.h"

//this line make this file to access the static parameter.
float AkOcclusionObstructionService::OcclusionFadeRate;

void UMyBlueprintFunctionLibrary::ChangeWwiseFadeRate(float rate)
{
    OcclusionFadeRate = rate;
}
Chinnawat Sirima
  • 410
  • 6
  • 10
0

It is hard to tell based on the code you posted but I assume the issue is because the line below is within a function:

float AkOcclusionObstructionService::OcclusionFadeRate = 3.0f;

Move that line outside the function and try it again. You can take a look at this question: How to access static members of a class?

Shak Ham
  • 1,209
  • 2
  • 9
  • 27
  • There's no reason to believe this is in a function. We're told that line is in the AkOcclusionObstructionService.cpp file. OcclusionFadeRate = rate; IS in a function, maybe that's what you meant. – Zebrafish Mar 06 '18 at 17:23