Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a constant array in 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++.

like image 455
hotdiggadydang Avatar asked May 17 '11 20:05

hotdiggadydang


People also ask

How do you create an array constant?

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. Of course, you should be able to initialize the array after the class as long as you use a static member variable.

What do you mean by constant array in C?

it's a constant array of integers i.e. the address which z points to is always constant and can never change, but the elements of z can change.

How do you declare an array constant in C++?

In C++, the most common way to define a constant array should certainly be to, erm, define a constant array: const int my_array[] = {5, 6, 7, 8};

Can I #define an array in C?

You can declare an array of any data type (i.e. int, float, double, char) in C.


2 Answers

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; } 
like image 44
Cubbi Avatar answered Sep 28 '22 17:09

Cubbi


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.

like image 193
Lightness Races in Orbit Avatar answered Sep 28 '22 17:09

Lightness Races in Orbit