resize vector from a library object

Status
Not open for further replies.

matoi

Member
Hi!

I'm trying to make use of a <vector> defined in an external .h library,
but that throws an error at compile time:

/var/folders/zh/bzyv3r3x00540nsr8zgwpvrh0000gp/T/arduino_build_4613/sketch/TEST_vector.ino.cpp.o: In function `std::vector<double, std::allocator<double> >::_M_check_len(unsigned int, char const*) const':
/Applications/Arduino.app/Contents/Java/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: error: ld returned 1 exit status
Error compiling for board Teensy 3.6.

I've stripped the code down as much as I could, and experimented with it, but to no success.
What puzzles me is that the vector defined within .ino file does want to 'resize' without troubles,
but the one defined in ABC.h makes trouble.
Could someone please explain what am I doing wrong. Thank you!

The code looks like this:

TEST_vector.ino
Code:
#include <vector>
#include "ABC.h"

using namespace std;

std::vector<double> myVector(10);

// myAbc contains a vector 'abcVector_'
ABC myAbc;

void setup() {

  // vector defined within .ino file does resize ok:
  myVector.resize(10);

  // attempt to resize a vector defined in external .h file
  // doesn't compile:
  myAbc.resizeAbcVector(10);
  
}

void loop() {
}

ABC.cpp
Code:
#include "ABC.h"

ABC :: ABC( void ){
  // empty for simplicity
}

void ABC :: resizeAbcVector ( unsigned long sizeRequest ){

  unsigned long currentSize = abcVector_.size();

  // this thing makes trouble:
  if ( sizeRequest > currentSize )
      abcVector_.resize( sizeRequest );
  
}

ABC.h
Code:
#ifndef ABC_H
#define ABC_H

#include <vector>

using namespace std;

class ABC {
  
  public:
    ABC( void );
    ~ABC(){};
    void resizeAbcVector ( unsigned long sizeRequest );
 
    std::vector<double> abcVector_;
};

#endif
 
What puzzles me is that the vector defined within .ino file does want to 'resize' without troubles,
That resize is using a compile time constant, the throw code path is optimized away.

but the one defined in ABC.h makes trouble.
The resize argument is not a compile time constant, the throw code path is included.

Look at these:
https://forum.pjrc.com/threads/23467-Using-std-vector?p=69787
https://github.com/bolderflight/Vector/blob/master/Vector.h
 
Many thanks! I don't really understand the story behind, but including that "Vector.h" in addition to <vector> seems to have solved the problem...
Thanks again!
Mato
 
Many thanks! I don't really understand the story behind, but including that "Vector.h" in addition to <vector> seems to have solved the problem...
Thanks again!
Mato

Glad it's working. You should be able to just include Vector.h now, it includes <vector> for you.
 
Status
Not open for further replies.
Back
Top