Hey,
I assume this isn’t cocos strictly associated so I posted in c++ discussion board.
I’m having bother understanding why this works:
// that is in a gameGlobals.h
struct GameGlobals
{
public:
static ParallaxInfiniteNode* level1ParallaxNode;
//...extra code
}
//that is within the supply file
#embody "gameglobals.h"
USING_NS_CC;
ParallaxInfiniteNode* GameGlobals::level1ParallaxNode = nullptr; // all good
//...extra code
but when I merely add this final line within the header file as so:
// that is in a gameGlobals.h
struct GameGlobals
{
public:
static ParallaxInfiniteNode* level1ParallaxNode;
//...extra code
}
ParallaxInfiniteNode* GameGlobals::level1ParallaxNode = nullptr; // ERROR
I’m not certain why assigning nullptr within the supply file make all the pieces work okay as anticipated, however taking that exact same line from the supply file to the header file fives a linking error (LNK1169: a number of multiply outlined symbols discovered.) It isn’t potential to initialize a static null pointer within the header?
You possibly can solely outline/initialize a static member discipline as soon as in a linked product/goal/executable, subsequently you have to be compiling and linking two or extra .cpp information that embody gameGlobals.h
and thus level1ParallaxNode will get outlined as many occasions because the variety of these .cpp information.
So once you solely outline it within the one .cpp supply file it really works since you’ve declared it throughout all .cpp information that embody the header (ie: translation models), however you solely outline it as soon as (and is thus solely outlined in a single .o object file).
Both (1) solely initialize it in a single .cpp supply file such as you did within the case that labored, or (2) make it inline and immediately initialize the worth inside the category discipline declaration itself, although I imagine this requires utilizing c++17?
inline static ParallaxInfiniteNode* level1ParallaxNode = nullptr;
1 Like
Improbable, thanks for the detailed rationalization.
This subject was robotically closed 24 hours after the final reply. New replies are not allowed.