user7082181
user7082181

Reputation: 364

c++ - expression did not evaluate to a constant - using constant from another class

[my code is quiet big, so I only pasted parts that seemed relevant to me, just tell me if you need to see more of it, thanks]

I defined a constant in a class (Animation) but now I split the class in two (AnimationsDefinition & AnimationsInstance), I can't use that constant from one class to the other

I included AnimationsDefinition.h before AnimationsInstance.h

common.h

#include "animationsDefinition.h"
#include "animationsInstance.h"

the compiler complains the constant cannot be evaluated

the below code used to be working, but now I use it in another class, it does not work anymore

the error I get :

error C2131: expression did not evaluate to a constant
note: failure was caused by non-constant arguments or reference to a non-constant symbol
note: see usage of 'EAST'

AnimationsDefinition.cpp

#include "common.h"

const int AnimationsDefinition::WEST    = 0;
const int AnimationsDefinition::SOUTH   = 1;
const int AnimationsDefinition::NORTH   = 2;
const int AnimationsDefinition::EAST    = 3;

...

AnimationsDefinition.h

#pragma once

class AnimationsDefinition
{
public:
    static const int WEST;
    static const int SOUTH;
    static const int NORTH;
    static const int EAST;
    ...

AnimationsInstance.cpp

#include "common.h"

void AnimationsInstance::update(float tpf)
{
    switch (direction)
    {
        case AnimationsDefinition::EAST: <<<<<<<<< compilation error
        {
            ...

Any help appreciated

regards

Upvotes: 1

Views: 2834

Answers (2)

eerorika
eerorika

Reputation: 238461

Const variables can be used as constant expressions only after their initialiser has been encountered. In this case, that can be achieved by moving the initialisers into the header.

Note that moving the initialiser into the declaration within the class definition in the header does not make the declaration into a definition of the variable. The definitions must still remain in the source file, just without the initialisers.

Ok I found out I could define these in the class itself since they are static

You can define them in the class itself (or the header in general) only if you declare them inline.


However, it is more conventional to use (scoped) enumerations for switch cases.

Upvotes: 2

P.W
P.W

Reputation: 26810

You can initialize the static const variables within the class itself.

class AnimationsDefinition
{
public:
    static const int WEST = 0;
    static const int SOUTH = 1;
    static const int NORTH = 2;
    static const int EAST = 3;
};

However, you would want to define them instead properly if you want to avoid ODR violations.

So use constexpr specifier which implies inline for static data members. And inline static data members can be defined in the class definition itself.

class AnimationsDefinition
{
public:
    static constexpr int WEST = 0;
    static constexpr int SOUTH = 1;
    static constexpr int NORTH = 2;
    static constexpr int EAST = 3;
};

As per [class.static.data]/3:

An inline static data member may be defined in the class definition and may specify a brace-or-equal-initializer.

Upvotes: 2

Related Questions