Static Const Variable Declaration

I may be off a bit here but in order to set a value on a const, I believe you have to assign it in the header rather than the .cpp. So you should declare and give initial value at the same time in your .h file.

Additionally, it’s probably better to write 1.0f rather than 1.f… (the same applies to 0.f and 10.f of course :slight_smile:

Hi, I am kind of newbie for C++. I actually come from JAVA world :slight_smile: The thing I am trying to do is to create some static and constant variables. However, I could not. Actually I succeeded when I create MIN_VOLUME variable only but it failed when I added more. Can someone lead me what the real problem is and how to succeed?

Header File:

class THECLOUD_API ACloud : public AActor
{
	GENERATED_UCLASS_BODY()

	static const float MIN_VOLUME;
	static const float MAX_VOLUME;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = FlyingObjects)
	float chargeLevel;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = FlyingObjects)
	float currentVolume;
	
};

CPP File:

#include "TheCloud.h"
#include "Cloud.h"

const float ACloud::MIN_VOLUME = 1.f;
const float ACloud::MAX_VOLUME = 10.f;

ACloud::ACloud(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	chargeLevel = 0.f;
	currentVolume = MIN_VOLUME;

}

Error I get:

error C2059: syntax error : 'constant'
error C2238: unexpected token(s) preceding ';'
error C2059: syntax error : 'constant'
error C2238: unexpected token(s) preceding ';'
error C2589: 'constant' : illegal token on right side of '::'
error C4091: '' : ignored on left of 'const float' when no variable is declared
error C2143: syntax error : missing ';' before '::'
error C2059: syntax error : '::'

ADDITIONAL INFO

  • Strange things happen. The code below compiles fine :confused: Is this a compiler bug issue or what?

    UCLASS()
    class ACloud : public AActor
    {
    GENERATED_UCLASS_BODY()

     const float VOLUME_MIN = 1.0f;
     const float VOLUME_MAX = 10.0f;
    
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = FlyingObjects)
     float chargeLevel;
    
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = FlyingObjects)
     float currentVolume;
    

    };

Your syntax looks correct. Did you by any chance leave off the UCLASS declaration from ACloud? If so, then perhaps GENERATED_UCLASS_BODY() is bogus and may be interfering with the compile. This is just a guess.

EDIT: Try removing the statics. You will get a different error that should be more indicative of what went wrong. My guess is that ACloud is not properly declared.

@anonymous_user_fcb7afa8 This code is good. Please check this: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

@anonymous_user_fcb7afa8 You can only set integral types like that, other types need a separate declaration and definition.

@ErayT The code you’ve posted seems fine to me. Is there anything else in the header that you haven’t posted here?

The following builds, so your statics themselves should be fine:

struct ACloud
{
	static const float MIN_VOLUME;
	static const float MAX_VOLUME; 
};

const float ACloud::MIN_VOLUME = 1.f;
const float ACloud::MAX_VOLUME = 10.f;

The code you’ve presented should compile correctly. My idea is that you have a typo somewhere else in the code, that will put parser into trouble. Optionally, please check THECLOUD_API definition, is this made by Epic ?

Yeah, that covers the static aspect of it, but not the const modifier: http://www.learncpp.com/cpp-tutorial/810-const-class-objects-and-member-functions/

All const variables must be
initialized at time of creation.

Yes. It is generated automatically by Epic. I also do not know why is that.

It’s for exporting the class outside of the module, so that other code can link against it.

Ok still same, nothing changed. I have no idea what is going on really :confused:
This is the error I get without statics:

error C2761: ‘const float
ACloud::MIN_VOLUME’ : member function
redeclaration not allowed

A couple of questions

If you mouse over the red squiggly under MAX_VOLUME what does the IDE say?

Do you have any includes in your Cloud.h? What is the order of them?

If you use different variable names that MIN_VOLUME and MAX_VOLUME does it make a difference?

MAX_VOLUME may already be defined for your class in a super class?

  1. It usually does not give anything meaningful.
    It says :

Error: Expected an identifier.

  1. The includes and the order are below :

    include “GameFramework/Actor.h”
    include “Cloud.generated.h”

  2. As I have mentioned in the post when I change the name “MAX_VOLUME” to just “MAX”, it compiles successfully. And this is msot strangest thing.

It seems that UE4\Engine\Source\Runtime\Engine\Public\Audio.h defines a macro called MAX_VOLUME… it might be conflicting with that (given your syntax highlighter made it purple, I’m guessing it is).

Could you try adding #undef MAX_VOLUME above your class declaration to see if the issue goes away then.

Impressive! I began to think of the idea that there was a bug issue in the compiler. I am glad to hear that there is something concrete reason behind that. The main problem is that visual studio does not assist great.