Need help with an error in my library

I’m writing a library for an I2C 16-bit I/O expander and getting the same error in two places. Both are in the .cpp file. The error is:

expected primary-expression before ‘register’

I’ve commented the lines where the errors are.

Here’s the .h :

#ifndef iox_h
#define iox_h

#include <WProgram.h>
#include <inttypes.h>
#include <avr/io.h>

#define INREG 0
#define OUTREG 2
#define INVREG 4
#define CFGREG 6

static int devAddr = 0x74;

class iox {
private:

public:
  int read(int register = INREG, int device = devAddr);
  void write(int dataIn, int register = OUTREG, int device = devAddr);
  void init(int configReg = 0xFFFF, int outputReg = 0xFFFF, int invertReg = 0x0000, int deviceAddress = 0x74);
};

extern iox IOX;
#endif

And the .cpp

#include "iox.h"
#include <avr/io.h>
#include <Wire.h>

int iox::read(int register, int device) {
  int dataOut = 0;
  Wire.beginTransmission(device);
  Wire.send(register);   /////// The compiler gives this line for the error ///////
  Wire.endTransmission();
  Wire.beginTransmission(device);
  Wire.requestFrom(device, 2);
  if (Wire.available()) {
    dataOut = Wire.receive();
  }
  if (Wire.available()) {
    dataOut |= (Wire.receive() << 8);
  }
  Wire.endTransmission();
  return dataOut;
}

void iox::write(int dataIn, int register, int device) {
  Wire.beginTransmission(device);
  Wire.send(register);   /////// The compiler also gives this line w/ same type of error ///////
  Wire.send(0xFF & dataIn);
  Wire.send(dataIn >> 8);
  Wire.endTransmission();
}

void iox::init(int configReg, int outputReg, int invertReg, int deviceAddress) {
  if (deviceAddress != 0x74) {
    devAddr = deviceAddress;
  }
  int invTemp = read(INVREG);
  if (invertReg != invTemp) {
    write(invertReg, INVREG);
  }
  int outTemp = read(OUTREG);
  if (outputReg != outTemp) {
    write(outputReg, OUTREG);
  }
  int cfgTemp = read(CFGREG);
  if (configReg != cfgTemp) {
    write(configReg, CFGREG);
  }
}

iox IOX = iox();

Can anyone see my mistake?

Thanks in advance for any help,
DJ

Try changing the word register to something else i.e. spell it differently. I think register is a reserved word.

Nope, I changed it to regVal and get the same error.

i just tried and it compiles ok

I changed all the 'register' with 'regVal' in both files.

Nope, I changed it to regVal and get the same error.

Nope, you didn't. I modified your library following Cloudy's advice and the error is gone.

Perhaps it's the IDE I'm using. I used AVR Project IDE. I'll try the library with the arduino IDE and let you know.

DJ

I found my "other" mistake. I didn't change the variable names in the function declarations ::). Thanks for the help guys, it works fine now! :)

I'm glad there's a forum for this stuff, DJ

If the lib is tested and stable (enough) it might be a good idea to write a playground article how to use the library .

@ robtillaart

I'm going to do more testing today. I only have a mega, but the I2C interface should be the same on other arduinos, correct? They just use different pins as I understand it. Anyway, after I've tested the library I'll write an example sketch with some documentation and put it on the playground for everyone. I'm pretty busy right now (X-mas and all) so I should have it posted in a day or two.

Thanks for everyones input, DJ

I only have a mega, but the I2C interface should be the same on other arduinos, correct? They just use different pins as I understand it.

Correct. Mega uses pin 20 for data and 21 for clock.