Hallo,
ich habe folgende function:
bool text_is_number(const char text[], int& value)
{
bool result = false;
value = 0;
if ((text != NULL) && (strlen(text) > 0))
{
result = true;
for (size_t i = 0; i < strlen(text); i++)
{
if (!isDigit(text[i]))
{
result = false;
break;
}
}
if (result) value = atoi(text);
}
return result;
}
Diese function wollte ich testweise auf eine foreach-Schleife umschreiben:
bool text_is_number_2(const char text[], int& value)
{
bool result = false;
value = 0;
if ((text != NULL) && (strlen(text) > 0))
{
result = true;
for (auto c : text)
{
if (!isDigit(c))
{
result = false;
break;
}
}
if (result) value = atoi(text);
}
return result;
}
Dann bekomme ich aber beim Compilieren folgende Fehlermeldungen:
sketch.ino: In function 'bool text_is_number_2(const char*, int&)':
sketch.ino:37:19: error: 'begin' was not declared in this scope
for (auto c : text)
^~~~
sketch.ino:37:19: note: suggested alternative: 'rewind'
for (auto c : text)
^~~~
rewind
sketch.ino:37:19: error: 'end' was not declared in this scope
sketch.ino:37:19: note: suggested alternative: 'rand'
for (auto c : text)
^~~~
randError during build: exit status 1
Um generell eine foreach-Schleife zu testen, hab ich Folgendes getestet, das auch funktioniert.
char type[11] = "sketch";
Serial.println(type);
for(char c:type)
{
if (c == NULL)
break;
Serial.println(c);
}
Serial.println(type);
Es liegt also an der umgeschriebenen function an den Übergabeparametern.
Was müsste man ändern, damit eine foreach-Schleife auch mit den Übergabeparametern funktioniert?