Error caused by including <vector>

I have encountered an error trying to compile code including vector on my core. Ive followed instructions for undefining min and max found elsewhere on this forum. I am unsure of the exact cause. Here is my code:

#include "application.h"
#undef min
#undef max
#include <vector>

unsigned long t = 0;
String ats, hts = "";
std::vector<String> games;
std::vector<String> names;
std::vector<int> scores;

void setup() {
    Serial.begin(115200);
    Spark.subscribe("hook-response/getScores", gotScoreData, MY_DEVICES);
}

void loop() {
    if (millis() >= t+30000) {
        Serial.println("Getting Scores...");
        Spark.publish("getScores");
        for (unsigned int x = 0; x < games.size(); x++ ) {
            Serial.println(names[x*2]+": "+scores[x*2]+" "+names[(x*2)+1]+": "+scores[(x*2)+1]);
        }
        t = millis();
    }
}

void gotScoreData(const char *name, const char *data) {
    String str = String(data);
    games.clear();
    names.clear();
    scores.clear();
    bool open = false;
    int start, end = 0;
    for (unsigned int x = 0; x < str.length(); x++) {
        if (str[x] == '{' && !open) {
            start = x;
            open = true;
        }
        else if (str[x] == '}' && open) {
            end = x;
            open = false;
            games.push_back(str.substring(start, end));
        }
    }
    for (unsigned int x = 0; x < games.size(); x++ ) {
        scores.push_back(tryExtractString(games[x], "\"ats\":", ",\"htc\"").toInt());
        scores.push_back(tryExtractString(games[x], "\"hts\":", ",\"atc\"").toInt());
        names.push_back(tryExtractString(games[x], "\"ata\":\"", ",\"rl\""));
        names.push_back(tryExtractString(games[x], "\"hta\":\"", ",\"ats\""));
    }
}

String tryExtractString(String str, const char* start, const char* end) {
    if (str == NULL) {
        return NULL;
    }

    int idx = str.indexOf(start);
    if (idx < 0) {
        return NULL;
    }

    int endIdx = str.indexOf(end);
    if (endIdx < 0) {
        return NULL;
    }

    return str.substring(idx + strlen(start), endIdx);
}

and here is the error:

In file included from ../inc/spark_wiring.h:29:0,
                 from ../inc/application.h:29,
                 from scoreboard.cpp:1:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
 #warning  "Defaulting to Release Build"
  ^
scoreboard.o: In function `construct<String, String>':
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/c++/4.8.4/ext/new_allocator.h:120: undefined reference to `String::String(String&&)'
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/c++/4.8.4/ext/new_allocator.h:120: undefined reference to `String::String(String&&)'
collect2: error: ld returned 1 exit status
make: *** [75b6353f2ed5d9d654352a53b6e0e4e63c4708485dbf97847e4dcb8cbe77.elf] Error 1

Any help is appreciated

Thanks in advance

I think this is your problem--try breaking this into two constructed objects:

String ats;
String hts;

Thanks @bko, but this does not appear to do it. Gives the same error. Also tried to remove these two variables completely, and it gives the same problem

Did you change this one too? Try this:

  String str(data);
  //...or...
  String str;
  str = data;

After a little bit of debugging, I’ve found that it’s caused by trying to insert a string into a vector, either using v.push_back() or v.insert()
It’s easily reproducible with this code:

#include "application.h"
#undef min
#undef max
#include <vector>

std::vector<String> v;
std::vector<int> v2;

void setup() {
    
}

void loop() {
    v2.push_back(2);
    // Works
    v.push_back("Hello World!");
    // Doesn't work
}

Still not sure of an actual cause though, or a fix

EDIT: I seem to have found at least a temporary fix, by using a pointer instead of a string

1 Like

Oh! I see. You should notice that the Arduino String object is not the same as the std::string. You could try

  std::vector<std::string> v;

This should work but I am not sure how well std::string works with the Wiring language since the Arduino String object is more popular.

Got it! Thanks, works like a charm