P
P
Pavel2016-04-04 16:57:18
C++ / C#
Pavel, 2016-04-04 16:57:18

Why does creating a static class member cause a linking error?

Created a project on cocos2d-x 3.10 with the aim of writing a remake of the Stack Attack game. I created a custom StackBlock class that describes the behavior of the box on the playing field and in the cocos2d-x main scene class I want to create a two-dimensional vector of pointers to the custom StackBlock class. + 2 auxiliary vectors, one of them contains the sum of blocks in a column, the other one contains the sum of blocks in a row.
0ede5b1008ef4dd0897d3d7b1f621635.jpg
As planned, the vector of rows will contain pointers to existing blocks or nullptr if there is no block. I thought that it would be more convenient if these vectors were created at the time of the scene initialization and further access to them would be from the functions of the StackBlock class and the player through the namespace of this scene, as is done with the scene initialization

Сlass EndlessMode : public cocos2d::Layer
{
public:
  
  static std::vector<std::vector<StackBlock*> > VVMainGrid;
  static std::vector<int> VIColumnWeight;
  static std::vector<int> VIRowWeight;

    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // implement the "static create()" method manually
    CREATE_FUNC(EndlessMode);
};

and the subsequent call directly to the function:
auto scene = EndlessMode::createScene();
But when I try to build the project, I get an error linking to these vectors.
Without declaring them static, the errors disappear ... But then I don’t understand how to access them, I don’t see where exactly an instance of the scene class is created. Why is this happening? What theory did I miss about static class members?
Repository here

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iv_k, 2016-04-04
@rusbaron

static members must be declared in the cpp file
like so.

std::vector<std::vector<StackBlock*> > EndlessMode::VVMainGrid;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question