[英]How to initialize a constant int to use for array size?
I have a static integer variable Game::numPlayers
, which is read in as an input from user. 我有一个静态整数变量
Game::numPlayers
,它是用户输入的内容。 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. 我显然不能仅仅分配
Game::numPlayers
的数组大小,并且我也尝试不初始化它,而让构造函数来工作,但这也不起作用。
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? 我只是复制一个值,它与
static const int nPlayers = 8
(将值8复制到nPlayers
并工作)有什么不同?
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 (eg 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. 为了澄清起见,我选择有一个向量数组,因为我希望每个节点都有一个单位的快速访问容器,但是每个用户/玩家一个容器,以便区分哪些单位属于每个节点内的哪个玩家(例如索引)数组的0 =玩家1,索引1 =玩家2,索引2 =玩家3,依此类推),否则我将只有一个向量或向量的向量。 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. 另外,
Game::numPlayers
作为用户输入被读取,但是在一个游戏循环中只能读取和分配一次,但是如果我关闭/重新启动/玩新游戏,则需要再次读取用户输入并分配一次。
Only integral constant expressions are allowed as array sizes in array declarations in C++. 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. 一个没有整数常量表达式的
const int
对象(由于它是从用户读取的,因此您的Game::numPlayers
并不存在)本身并不具有整数常量表达式的资格。
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 ++中的数组声明中。 C++11 does support some semblance of C99-style Variable Length Arrays, but your case (a member array) is not covered by it anyway.
C ++ 11确实支持C99样式的可变长度数组,但无论如何您的情况(成员数组)都不会被覆盖。
If the array size is a run-tuime value, use std::vector
. 如果数组大小是运行时值,请使用
std::vector
。 In your case that would become std::vector
of std::vector
s. 在您的情况下,它将变成
std::vector
的std::vector
。
I don't see why you need to use an array of std::vector
if the number of elements will be obtained at runtime. 我不明白,如果要在运行时获取元素数量,为什么需要使用
std::vector
数组。
Instead, create a std::vector<std::vector<Units>>
and size it appropriately on construction. 而是创建一个
std::vector<std::vector<Units>>
并在构造时适当调整其大小。 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. 该
vector
通过调用已经知道自己的大小vector::size()
函数。 Carrying around unnecessary variables that supposedly gives you this information opens yourself up for bugs. 携带不必要的变量(可能会为您提供此信息)可以使您自己发现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.