How to initialize a constant int to use for array size?

谁说我不能喝 提交于 2021-01-28 10:23:37

问题


I have a static integer variable Game::numPlayers, which is read in as an input from user. I then have the following class defined as such:

class GridNode
{
private:
    /* Members */
    static const int nPlayers = Game::numPlayers;
    std::vector<Unit> units[nPlayers];

    //...
};

This won't compile ("in-class initializer for static data member is not a constant expression").

I obviously cant just assign the array size of Game::numPlayers, and I also tried not initializing it and letting a constructor do the work, but that didn't work either.

I don't understand what I'm doing wrong here and what else I could possibly do to get this to work as intended.

I'm just copying a value, how is that any different from static const int nPlayers = 8 which copies the value 8 into nPlayers and works?

Edit:

To clarify, I choose to have an array of vectors because I want each node to have a quick-access container of units, but one container for each user/player so as to distinguish which units belong to which player within each node (e.g. index 0 of the array = player 1, index 1 = player 2, index 2 = player 3, and so on), otherwise I would just have one vector or a vector of vectors. I thought a map might work, but I thought an array of vectors would be faster to access and push into.

Also, Game::numPlayers is read in as a user input, but only read and assigned once within one game loop, but if I close/restart/play a new game, it needs to read in the user input again and assign it once.


回答1:


I don't see why you need to use an array of std::vector if the number of elements will be obtained at runtime.

Instead, create a std::vector<std::vector<Units>> and size it appropriately on construction. if you need to reset the size, have a member function resize the vector.

Example:

class GridNode
{
    private:
        /* Members */
        std::vector<std::vector<Unit>> units;

    public:
        GridNode(int nPlayers=10) : units(nPlayers) {}

        std::vector<Unit>& get_units(size_t player) 
        { return units[player]; }  // gets the units for player n

        void set_num_players(int nPlayers) 
        {  units.resize(nPlayers); }  // set the number of players

        int get_num_players() const { return units.size(); }
 };

 int main()
 {
     int numPlayers;
     cin >> numPlayers;
     //...
     GridNode myGrid(numPlayers); // creates a GridNode with 
                                    // numPlayers vectors.  
     //...
     Unit myUnit;
     myGrid.get_units(0).push_back(myUnit); // places a Unit in the 
                                            // first player
 }

Also, it isn't a good idea to have extraneous variables tell you the vector's size. The vector knows its own size already by calling the vector::size() function. Carrying around unnecessary variables that supposedly gives you this information opens yourself up for bugs.




回答2:


Only integral constant expressions are allowed as array sizes in array declarations in C++. A const int object initailized with something that is not an integral constant expression (your Game::numPlayers is not, since it is read from the user), does not itself qualify as integral constant expression.

The bottom line here is that regardless of how you slice it, it is not possible to sneak in a run-time value into an array declaration in C++. C++11 does support some semblance of C99-style Variable Length Arrays, but your case (a member array) is not covered by it anyway.

If the array size is a run-tuime value, use std::vector. In your case that would become std::vector of std::vectors.



来源:https://stackoverflow.com/questions/31226528/how-to-initialize-a-constant-int-to-use-for-array-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!