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.
Attempt: initializing in the constructor of the object.
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?
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
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:
Because it is declared as a local variable. Instead of assigning you are declaring AND assigning.
Compare two pieces of code:
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
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.
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.