-1

I would like to initialize a normal array in C++.

class Test {

    public: int a[5];
    a[0] = {1}; // or simply a[0] = 1;
    
};

int main(){

    Test Obj;
    cout<<Obj.a[0];
}

It gives an error "a does not name a type". There is another method which I'm aware of: initializing using constructor or using member function. My entire point of asking is, why should I use any getter and setter methods just to initialize the normal array? Does it break any C++ rule and why am I getting this error?

  • 1
    https://stackoverflow.com/questions/33991363/is-it-possible-to-initialize-an-array-in-a-member-initializer-list-in-c does this answer it? – Hatted Rooster Apr 02 '21 at 17:55
  • What exactly are you trying to do? Why not just do `int a[5]{1};`? – cigien Apr 02 '21 at 17:56
  • cigien, i was trying to initialize the array in that way we do the in c or out of the class in ++ – Harshit Singh Apr 02 '21 at 18:26
  • That would be assignment, not initialization. See https://stackoverflow.com/questions/23850656/assigning-one-array-to-another-array-c – cigien Apr 02 '21 at 19:12

2 Answers2

1

You can initialize the array like this in the same line

class Test{

public: 
int a[5] = {1};
};

it will set the first value with 1.

Eman
  • 56
  • 6
  • sorry to say but it will set all value of entire array is 1 which means a[0]= 1 and a[1] = 1 and so on.. until the a[5], that is not what i'm trying to do here, i want to set all the index values individually, i mean one by one . – Harshit Singh Apr 02 '21 at 18:10
  • I actually tried it and it didn't, it set only the first one with 1 – Eman Apr 02 '21 at 18:11
  • my question got marked as duplicate, i searched on the internet how to do this simple thing, but badly there is no answer. – Harshit Singh Apr 02 '21 at 18:21
  • @HãŕşhįțŚïñğh I don't follow. This solution seems to do exactly what you want. What is the issue with it? – cigien Apr 02 '21 at 18:26
  • @HãŕşhįțŚïñğh you can do that like this: int a[5] = {1,2,3,4,5}; does this answer your question? – Eman Apr 02 '21 at 18:27
  • actually my main focus was to know why i was getting that error, when i was trying to initializing in that way ? just because whats c++ has been thinking is , when it was trying to compile the line a[0] = 2; is, c++ should have to declare the array with the name a and assign the value of 2 in the 0th index, but as we can see there is no data type, that is why its throwing an error beacuse of missing the data type. and what i was trying to do is first declare the array with the appropriate size and than assign the array values with their index later on. hope you guys have a idea of mine now. – Harshit Singh Apr 02 '21 at 18:53
0

You could use a constructor:

class Test{

public: int a[5];

  Test() {a[0]=1; a[1]=5; a[2]=4; a[3]=8; a[4] = 9;}
    }
};
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Patricio Loncomilla
  • 1,048
  • 3
  • 11
  • "*must*" is a bit strong, as that requirement hasn't been needed since C++11 added the ability to initialize class members directly in their declarations. – Remy Lebeau Apr 02 '21 at 18:07
  • why should i use the constructor here ? does c++ not allow that kind of initialization which I'm doing in my code ? – Harshit Singh Apr 02 '21 at 18:13