-4

I am using this function to copy one vector to the end of another:

template <typename T, typename U>
void addVectorToVector(std::vector<T>* oldVector, std::vector<U> input){
  for(uint8_t i = 0; i < Input(size); i++){
    T inputVar = (T) Input[i];
    oldVector->push_back(inputVar);
  }
return;
}

This throws a compilation error in Arduino IDE:

C:\Users\ehle_ta\AppData\Local\Temp\arduino_build_330682\sketch\main.ino.cpp.o: In function `std::vector<unsigned char, std::allocator<unsigned char> >::_M_check_len(unsigned int, char const*) const':

c:\program files (x86)\arduino\hardware\tools\arm\arm-none-eabi\include\c++\5.4.1\bits/stl_vector.h:1425: undefined reference to `std::__throw_length_error(char const*)'

collect2.exe: error: ld returned 1 exit status

I can tell that the error dissappears when I comment out

oldVector->push_back(inputVar);

but I don't get why does this link error happen.

edit:

The error can be triggered by compiling this code:

#include <vector>
void setup() {
  std::vector<unsigned char> vector1;
  std::vector<unsigned char> vector2;
  vector2.push_back(0x01);
  vector2.push_back(0x02);
  addVectorToVector(&vector1, vector2); 
}
// add the content of a vector to the end of another vector
template <typename T, typename U>
void addVectorToVector(std::vector<T>* oldVector, std::vector<U> input){ 
  for(uint8_t i = 0; i < input.size(); i++){
    T inputVar = (T) input[i];
    oldVector->push_back(inputVar);
  }
  return;
}

Is there an error in my implementation/use of vector::push_back or is possible that the std::vector class I'm using isn't the Standard C++11 one and therefore causes problems?

tobeh
  • 1
  • 5
  • 1
    What is `Input`? Is that supposed to be `input`? What is `Input(size)`? Please post a [mcve]. Why can't you use [`vector::insert`](https://en.cppreference.com/w/cpp/container/vector/insert)? – Praetorian Mar 07 '19 at 14:36
  • Welcome to Stack Overflow! Please **[edit]** your question with an [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Mar 07 '19 at 14:36
  • how can you compile that code ? `Input(size)` is `input.size()` ? `Input` is `input` (but the type is U and T when getting element) ? Are you sure you give us the original code you compiled then linked ? An `uint8_t` for the index is not a good choice. – bruno Mar 07 '19 at 14:36
  • The error is that the linker cannot find an STL function that throws the length error (judging by its name), not the length error itself. This must be a bytecode coruuption in the STL `.lib` files, since only a part of STL is missing. – Kotauskas Mar 07 '19 at 14:37
  • 1
    Possible duplicate of [Vectors in Arduino](https://stackoverflow.com/questions/9986591/vectors-in-arduino) and [Is the C++ STL fully supported on Arduino?](https://arduino.stackexchange.com/questions/24790/is-the-c-stl-fully-supported-on-arduino). – O'Neil Mar 07 '19 at 14:44
  • For what it's worth this is the kind of error typically seen when trying to compile/link `C++` source using a `C` compiler resulting in linking against the `C` standard lib rather than the `C++` equivalent. – G.M. Mar 07 '19 at 14:49
  • I thought the cryptic error message was caused by the fact that Arduino doesn't Support the C++ exception/try/catch System. I am using a fresh Arduino Installation with the Teensyduino Addon. – tobeh Mar 07 '19 at 14:51
  • @tobeh you cannot use `std::vector` in the arduino IDE without somehow importing the standard library yourself. See O'Neil's comment. – Sailanarmo Mar 07 '19 at 16:54

1 Answers1

0

Try this

template <typename T, typename U>
void addVectorToVector(std::vector<T>* oldVector, std::vector<U> &input) 
{
    for (int i = 0; i < input.size(); i++) {
        T inputVar = (T) input[i];
        oldVector->push_back(inputVar);
    }

}
Aris
  • 138
  • 1
  • 12