# Problem: initializing member array before loop

**URL:** <https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336>\
**Category:** Programming\
**Created:** [September 4, 2025, 9:30am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336 "2025-09-04T09:30:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 9:30am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/1 "2025-09-04T09:30:04Z")

</div>

Hello dear community,

I´ve been experiencing a scope problem that I don´t know how to solve.  
I am trying to initialize an object member before the loop(). I tried different ways, but all of them return 0 even though I´m convinced, that the actual inititlization is okay. I'm not getting any compile errors, I simply get a zero, where I think I already assigned a value.

1. Attempt: initializing in the constructor of the object.

```arduino
//----Obj.h
#define SIZE 5

class Obj
{
  public:
  Obj();
  void getval(uint8_t pos);

  private:
  std::array <uint8_t, SIZE> member;

//----Obj.cpp
Obj::Obj()
{
  std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
}

void getval(uint8_t pos) {
  Serial.println("Loop: "+String(member[pos]));
}

//----main.ino
Obj MyObj();

setup(){
  Serial.begin(9600);
}

loop() {
  MyObj.getval (0);
}

```

Result:  
Loop: 0  
Loop: 0  
Loop: 0  
...

1. Attempt: initializing in function in setup.

```arduino
//----Obj.h
#define SIZE 5

class Obj
{
  public:
  Obj();
  void begin();
  void getval(uint8_t pos);

  private:
  std::array <uint8_t, SIZE> member;

//----Obj.cpp
Obj::Obj() {}

void begin() {
  std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
  Serial.println("Setup: "+String(member[1]));
}

void getval(uint8_t pos) {
Serial.println("Loop: "+String(member[pos]));
}

//----main.ino
Obj MyObj();

setup(){
  Serial.begin(9600);
  MyObj.begin();
}

loop() {
  MyObj.getval (1);
}

```

Result:  
Setup: 1  
Loop: 0  
Loop: 0  
...

I really want to avoid putting the begin function in the main loop (didn't even try it yet), since the array needs to be non-static and non-const later on in the project.  
From my understanding the obejct is global, hence the members should be too (except of being private), so this should work.  
Where am I wrong?

Thanks in advance  
Cheers  
Jokinger

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 4, 2025, 10:08am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/2 "2025-09-04T10:08:51Z")

</div>

the posted code had numerous errors

here's a re-write the generated the following

output

```arduino
Loop: 1
Loop: 2
Loop: 3
Loop: 99
Loop: 2001

```

```arduino
#define SIZE 5

class Obj {
    int member [SIZE] = { 1, 2, 3, 99, 2001 };

  public:
    Obj () { }

    void getval (uint8_t pos) {
        Serial.print ("Loop: ");
        Serial.println (member[pos]);
    }
} myObj;

// -----------------------------------------------------------------------------
void setup (){
    Serial.begin (9600);

    for (int n = 0; n < SIZE; n++)
        myObj.getval (n);
}

void loop () {
}

```

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [September 4, 2025, 10:27am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/3 "2025-09-04T10:27:41Z")

</div>

> [@jokinger](#):
>
> ```arduino
> Obj::Obj()
> {
> std::array <uint8_t, SIZE> member = {0, 1
> 
> ```

I am no expert on C++ but it looks like a local variable that simply shadows your real “member“.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 4, 2025, 10:31am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/4 "2025-09-04T10:31:13Z")

</div>

errors from posted code

```arduino
Tst:11:8: error: 'array' in namespace 'std' does not name a template type
   std::array <uint8_t, SIZE> member;
        ^ ~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:14:1: warning: extra qualification 'Obj::' on member 'Obj' [-fpermissive]
 Obj::Obj()
 ^~~
Tst:14:1: error: 'Obj::Obj()' cannot be overloaded
Tst:7:3: error: with 'Obj::Obj()'
   Obj();
   ^~~
Tst:19:6: error: 'void Obj::getval(uint8_t)' cannot be overloaded
 void getval(uint8_t pos) {
      ^ ~~~~~
Tst:8:8: error: with 'void Obj::getval(uint8_t)'
   void getval(uint8_t pos);
        ^ ~~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:26:7: warning: ISO C++ forbids declaration of 'setup' with no type [-fpermissive]
 setup(){
       ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:30:6: warning: ISO C++ forbids declaration of 'loop' with no type [-fpermissive]
 loop() {
      ^
Tst:32:1: error: expected '}' at end of input
 }
 ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In constructor 'Obj::Obj()':
Tst:16:8: error: 'array' is not a member of 'std'
   std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
        ^ ~~~~
Tst:16:22: error: expected primary-expression before ',' token
   std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
                      ^
Tst:16:30: error: 'member' was not declared in this scope
   std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
                              ^ ~~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:16:30: note: suggested alternative: 'memmem'
   std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };
                              ^ ~~~~~
                              memmem
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In member function 'void Obj::getval(uint8_t)':
Tst:20:34: error: 'member' was not declared in this scope
   Serial.println("Loop: "+String(member[pos]));
                                  ^ ~~~~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:20:34: note: suggested alternative: 'memmem'
   Serial.println("Loop: "+String(member[pos]));
                                  ^ ~~~~~
                                  memmem
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In member function 'int Obj::setup()':
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:28:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In member function 'int Obj::loop()':
Tst:31:3: error: invalid use of member function 'Obj Obj::MyObj()' (did you forget the '()' ?)
   MyObj.getval (0);
   ^ ~~~~
Tst:31:3: error: invalid use of member function 'Obj Obj::MyObj()' (did you forget the '()' ?)
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: At global scope:
Tst:32:1: error: expected unqualified-id at end of input
exit status 1
'array' in namespace 'std' does not name a template type

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 4, 2025, 10:31am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/5 "2025-09-04T10:31:23Z")

</div>

> [@vvb333007](#):
>
> it looks like a local variable that simply shadows your real “member“.

Yes, [Variable Shawdowing](https://en.wikipedia.org/wiki/Variable_shadowing).  
The local variable being initalized is destroyed when the function (constructor or begin) ends.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [September 4, 2025, 10:37am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/6 "2025-09-04T10:37:31Z")

</div>

It still works with std::array

```arduino
/* Online C++ Compiler and Editor */
#include <iostream>
#include <cstdint>

#include <algorithm>
#include <array>
#include <iterator>
#include <string>

using namespace std;

#define SIZE 5

class Obj
{
  public:

  //private:
  std::array <uint8_t, SIZE> member = {0, 1, 2, 3, 4 };

} ;

Obj myObj ;

int main()
{
    for ( int i = 0; i < SIZE ; i++ ) {
       std::cout << (uint16_t)myObj.member[i] << endl; 
    }   
   return 0;
}

```

---

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 11:15am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/8 "2025-09-04T11:15:35Z")

</div>

Thank you. You´re right about the mistakes. I only posted it as an example.  
But you are using the variable public, which I don´t attend to declare it.

---

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 11:18am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/9 "2025-09-04T11:18:52Z")

</div>

> [@gfvalvo](#):
>
> > [@vvb333007](#):
> >
> > it looks like a local variable that simply shadows your real “member“.
> 
> Yes, [Variable Shawdowing](https://en.wikipedia.org/wiki/Variable_shadowing).  
> The local variable being initalized is destroyed when the function (constructor or begin) ends.

Why is it local though?  
myObj is initialized before the setup, so it should be global.  
Thanks for the link. I´ll browse through it

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 4, 2025, 11:19am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/10 "2025-09-04T11:19:02Z")

</div>

> [@jokinger](#):
>
> Thank you. You´re right about the mistakes. I only posted it as an example.

If you're asking for (free) help, common courtesy would suggest that you post your actual code (or an abbreviated) version so that members don't waste time fixing errors that aren't part of your problem.

Regarding initialization ... most efficient would be an initializer list in the constructor:

```arduino
#define SIZE 5

class Obj {
  public:
    Obj() : member ( {
      0, 1, 2, 3, 4
    }) {};
    uint8_t getVal(uint8_t pos);

  private:
    std::array <uint8_t, SIZE> member;
};

uint8_t Obj::getVal(uint8_t pos) {
  return member[pos];
}

Obj myObj;

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  for (size_t i = 0; i < SIZE; i++) {
    Serial.println(myObj.getVal(i));
  }
}

void loop() {
}

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 4, 2025, 11:20am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/11 "2025-09-04T11:20:39Z")

</div>

> [@jokinger](#):
>
> Why is it local though?

You've defined it inside the function. That makes it local.

---

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 11:28am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/12 "2025-09-04T11:28:40Z")

</div>

There's a bit more to it, so I didn't want to bother you with that stuff, but yes, thank you. I´ll keep it in mind for the next time.

---

<div class="post-metadata">

**Author:** ![vvb333007](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/vvb333007/32/974521_2.png) [@vvb333007](https://forum.arduino.cc/u/vvb333007)\
**Post date:** [September 4, 2025, 11:36am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/14 "2025-09-04T11:36:58Z")

</div>

> [@jokinger](#):
>
> Why is it local though?

Because it is _declared_ as a local variable. Instead of _assigning_ you are _declaring_ AND assigning.

Compare two pieces of code:

```arduino
int Global_variable = 10;

void some_function() {

  int Global_Variable = 666;
  Serial.printf("some_function() : Global_Variable is %d\r\n", Global_Variable);
}

void loop() {
  Serial.printf("loop() : Global_Variable is %d\r\n", Global_Variable);
  some_function();
  Serial.printf("loop() : Global_Variable is %d\r\n", Global_Variable);

  delay(1000);
}

```

And

```arduino
int Global_variable = 10;

void some_function() {

  Global_Variable = 666;
  Serial.printf("some_function() : Global_Variable is %d\r\n", Global_Variable);
}

void loop() {
  Serial.printf("loop() : Global_Variable is %d\r\n", Global_Variable);
  some_function();
  Serial.printf("loop() : Global_Variable is %d\r\n", Global_Variable);

  delay(1000);
}

```

Note that in second code fragment, the Global\_Variable is assigned in a right way. In first code example the same variable is _declared local in the function._

---

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 11:39am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/15 "2025-09-04T11:39:54Z")

</div>

Yeah, I saw it when you wrote it.  
Thank you.  
Fun fact: I was taught wrong but never had problems with it, because I rarely used different intial values than zero.

---

<div class="post-metadata">

**Author:** ![jokinger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jokinger/32/1126736_2.png) [@jokinger](https://forum.arduino.cc/u/jokinger)\
**Post date:** [September 4, 2025, 11:42am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/16 "2025-09-04T11:42:07Z")

</div>

Thank you very much for elaborating. You´re absolutely right.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 3, 2026, 11:42am UTC](https://forum.arduino.cc/t/problem-initializing-member-array-before-loop/1405336/17 "2026-03-03T11:42:53Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
