在 c++ 中制作常量数组

发布于 2024-11-07 12:32:37 字数 259 浏览 3 评论 0原文

代码块告诉我无法创建数组有什么原因吗?我只是想做:

const unsigned int ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};

它给了我

错误:在“{”标记之前不允许使用大括号括起来的初始值设定项

我已经更改了初始化程序的其他部分,但错误总是说同样的事情。这似乎没有意义,因为这是我在 C++ 中学到的第一个东西。

Is there any reason why codeblocks is telling me that I can't make an array? I'm simply trying to do:

const unsigned int ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};

and it's giving me

error: a brace-enclosed initializer is not allowed here before '{' token

I have changed other parts of the initializer, but the error is always saying the same thing. This doesn't seem to make sense, since this is one of the first things I learned in c++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-11-14 12:32:37

您说您在类中作为私有变量执行了此操作。

回想一下(目前),成员变量可能不会在声明它们的同一位置进行初始化(有一些例外)。

struct T {
   std::string str = "lol";
};

不行。它必须是:

struct T {
   std::string str;
   T() : str("lol") {}
};

但是,雪上加霜的是,在 C++0x 之前,您无法在构造函数初始化器中初始化数组!:

struct T {
   const unsigned int array[10];
   T() : array({0,1,2,3,4,5,6,7,8,9}) {} // not possible :(
};

而且,因为数组的元素是 const code>,你也不能依赖赋值:

struct T {
   const unsigned int array[10];
   T() {
       for (int i = 0; i < 10; i++)
          array[i] = i; // not possible :(
   }
};

然而,正如其他一些贡献者非常正确地指出的那样,如果可以的话,为每个 T 实例拥有一个数组副本似乎没有什么意义不要修改它的元素。相反,您可以使用static 成员。

因此,以下内容将最终以可能是最好的方式解决您的问题:

struct T {
   static const unsigned int array[10];
};

const unsigned int T::array[10] = {0,1,2,3,4,5,6,7,8,9};

希望这会有所帮助。

You say that you did this within a class, as a private variable.

Recall that (at the moment), member variables may not be initialised in the same place where you declare them (with a few exceptions).

struct T {
   std::string str = "lol";
};

is not ok. It has to be:

struct T {
   std::string str;
   T() : str("lol") {}
};

But, to add insult to injury, pre-C++0x you cannot initialise arrays in the ctor-initializer!:

struct T {
   const unsigned int array[10];
   T() : array({0,1,2,3,4,5,6,7,8,9}) {} // not possible :(
};

And, because your array's elements are const, you can't rely on assignment either:

struct T {
   const unsigned int array[10];
   T() {
       for (int i = 0; i < 10; i++)
          array[i] = i; // not possible :(
   }
};

However, as some other contributors have quite rightly pointed out, there seems little point in having a copy of the array for each instance of T if you can't modify its elements. Instead, you could use a static member.

So, the following will ultimately solve your problem in what's — probably — the best way:

struct T {
   static const unsigned int array[10];
};

const unsigned int T::array[10] = {0,1,2,3,4,5,6,7,8,9};

Hope this helps.

太傻旳人生 2024-11-14 12:32:37

由于这是类中的私有成员变量(根据注释),这在C++03中确实是不允许的。

许多现代编译器部分支持的 C++0x 允许编译以下内容:

class C
{
    const unsigned int ARRAY[10];
 public:
    C() : ARRAY{0,1,2,3,4,5,6,7,8,9} {}
};
int main()
{
    C obj; // contains a non-static const member: non-assignable 
}

但是,非静态 const 成员只有在类的不同实例中包含不同值时才有意义。如果每个实例都包含相同的 {0,1,2,3,4,5,6,7,8,9},那么您应该将其设为 static ,这也使得在 C++98 中执行此操作成为可能:

class C
{
    static const unsigned int ARRAY[10];
 public:
    C() {}
};
const unsigned int C::ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};
int main()
{
    C obj;
}

Since this is a private member variable in a class (according to the comment), this is indeed not allowed in C++03.

C++0x, partially supported by many modern compilers, allows the following to compile:

class C
{
    const unsigned int ARRAY[10];
 public:
    C() : ARRAY{0,1,2,3,4,5,6,7,8,9} {}
};
int main()
{
    C obj; // contains a non-static const member: non-assignable 
}

However, non-static const members only make sense if they contain different values in different instances of the class. If every instance is to contain the same {0,1,2,3,4,5,6,7,8,9}, then you should make it static, which also makes it possible to do this in C++98:

class C
{
    static const unsigned int ARRAY[10];
 public:
    C() {}
};
const unsigned int C::ARRAY[10] = {0,1,2,3,4,5,6,7,8,9};
int main()
{
    C obj;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文