-6

I am trying to combine integers x y z from an accelerometer on microbit into a string and then send it to serial port. I am using c++ in online mbed compiler here with the microbit DAL library.

uBit.init();
uBit.serial.baud(115200);

MicroBitI2C i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0); 
MicroBitAccelerometer accelerometer = MicroBitAccelerometer(i2c);




while(1) {

    int x=uBit.accelerometer.getX();
    int y=uBit.accelerometer.getX();
    int z=uBit.accelerometer.getX();

    stringstream result;
    result << x << "," << y << "," << z;
    uBit.serial.send(result.c_str());
    uBit.serial.send("\r\n");
}

however the result.c_str() gave me an error of Error: Class "std::basic_stringstream, std::allocator>" has no member "c_str" in "main.cpp", Line: 26, Col: 34 screenshot

1 Answers1

3

That probably is because the method send only takes a const char* as argument and not a std::string. Try:

uBit.serial.send(result.c_str());

edit:

Now your code has changed and result is a stringstream:

uBit.serial.send(result.str().c_str()).

Benjamin Barrois
  • 1,835
  • 5
  • 15
  • sorry its not working.. it says Error: Class "std::basic_stringstream, std::allocator>" has no member "c_str" in "main.cpp", Line: 26, Col: 34 – Beats Away Mar 15 '18 at 13:24
  • Did you really enter `result.c_str()` and not `result.c_str`? – Benjamin Barrois Mar 15 '18 at 13:26
  • nope.. i just updated the question yea i used result.c_str() but still not correct – Beats Away Mar 15 '18 at 13:33
  • See my updated answer then. – Benjamin Barrois Mar 15 '18 at 13:48
  • thanks! now no lines is marked as error but tons of alerts saying ..Error: No space in execution regions with .ANY selector matching https://imgur.com/a/Wr0WQ – Beats Away Mar 15 '18 at 14:48
  • The problem seems to be that your target has not enough RAM for what you are trying to do: http://www.keil.com/forum/19067/ Please accept my answer as the solution to your initial problem if it indeed solved it. Thanks! – Benjamin Barrois Mar 15 '18 at 15:00
  • thanks.. it solved the script problem but doesnt work on my hardware.... i feel bad to approve this answer but yes u are right u have answered it.. – Beats Away Mar 15 '18 at 15:08
  • Well you can still ask a new question with your new problem if you want an answer, but probably on another place (like maybe superuser.com), this is not in stackexchange scope. – Benjamin Barrois Mar 15 '18 at 15:16