Hello everyone,
I am having a massive headache as to figuring out why this is occuring, but this is probably my ignorance of C++. I am trying to access a specific element in a vector list of strings, but every time I compile the code:
#include <vector>
void setup() {
Serial.begin(9600);
}
void loop() {
String change = "S0false, S1true, S2false, S3false, S4true";
char cArray[change.length() + 1];
strcpy(cArray, change.c_str());
char* changes = strtok(cArray, ", ");
std::vector<String> arr;
while (changes != NULL) {
arr.push_back(changes);
changes = strtok(NULL, ", ");
}
if (!trueFalse(arr.at(0))) {
Serial.print('f');
}
}
bool trueFalse(String test) {
return test.charAt(2) == 't';
}
I get the following error:
sketch\sketch_dec29a.ino.cpp.o: In function `std::vector<String, std::allocator<String> >::_M_range_check(unsigned int) const':
c:\users\kyle\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\c++\4.8.3\bits/stl_vector.h:794: undefined reference to `std::__throw_out_of_range(char const*)'
collect2.exe: error: ld returned 1 exit status
But when I change
if (!trueFalse(arr.at(0))) {
Serial.print('f');
}
to:
for (int i = 0; i < arr.size(); i++) {
if (!trueFalse(arr.at(i))) {
Serial.print(i);
}
}
It compiles without a problem. I'm not sure if there's a misunderstanding that I am not seeing, or what is going on. If anyone is able to help out, I'd greatly appreciate it.
Thank you,
-Kyle