Compiler can't find any function called i2c.read() in a sensor library

Using help from this forum, I managed to port an Arduino BMX160 Library DFRobot_BMX160.h to mbed. However, there’s one single error that happens when compiling the program:

Error: No matching member function for call to 'read' in "DFBot/DFBot/DFRobot_BMX160.cpp", Line: 317, Col: 9

The line: i2c.read(_addr << 1, pBuf, len);

The function:

void DFRobot_BMX160::readReg(uint8_t reg, uint8_t *pBuf, uint16_t len)
{

    i2c.lock();
    i2c.start();

    // to indicate an i2c read, shift the 7 bit address up 1 bit and set bit 0 to a 1
    i2c.write(_addr << 1 | 1);

    int writeResult = i2c.write(reg);
    i2c.stop();
    i2c.unlock();
    if(writeResult != 1) {
        return;
    }

    i2c.read(_addr << 1, pBuf, len);

}

The error does not occur for any other i2c related functions (e.g. i2c.start(), i2c.lock(), i2c.write() etc.) i2c has been defined at the beginning of the library cpp file as: I2C i2c(PC_1,PC_0);

Included headers in “DFRobot_BMX160.h”:
#include “stdint.h”
#include “stdlib.h”
#include “mbed.h”
#include “platform/mbed_thread.h”

The entire code compiles when i comment out that single line.

Note that the I2C functions are being defined inside the library cpp file and not main file.

Ahoj,

I do not know what tool you use but it want to tell you about a conflict in data types and because of that it can not find any corresponding candidate.
Mbed’s I2C documentation say:

int read ( int address ,
char * data ,
int length ,
bool repeated = false
)

So you need converting/retype your variables. Something similar like below (not tested).

i2c.read((int)_addr << 1, (char*)pBuf, (int)len);

BR, Jan

1 Like

That worked. Thanks.

From the C++ Standard:

“1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.”

If you want to make it obvious that a variable is a class member variable you could just prepend ‘this->’. The compiler will throw a fit if you make a typing mistake and the variable is not a member.