Search code examples
c++mbedbbc-microbit

Why argument accept "text" but not string variable?


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


Solution

  • 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()).