0

I feel like I'm missing something obvious here and need it pointed out. I'm trying to create an instance of a struct that's a member of a class contained within a library that's been included in the current file, but inside a class I've defined myself. If I do this outside of the class, it works, but inside the class it appears to try to instantiate the struct as if it was a member of the parent class it's in.

The library is for a BME280 sensor and the struct is to pass chip settings to the constructor of the main class that drives the sensor. Not even sure how to word this problem, but hopefully the code illustrates what I'm trying to say.

If I instantiate this struct outside of a class it works fine,

#include <BME280I2C.h>

// BME280 sensor settings
BME280I2C::Settings settings(
  BME280::OSR_X16,
  BME280::OSR_X16,
  BME280::OSR_X16,
  BME280::Mode_Forced,
  BME280::StandbyTime_1000ms,
  BME280::Filter_Off,
  BME280::SpiEnable_False,
  BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
);

class UsermodBME280 : public Usermod {
  private:
    BME280I2C bme(settings);
};

but if I try to do this within the class it seems to try and instantiate the struct as if it was a member of the UsermodBME280 class itself.

#include <BME280I2C.h>

class UsermodBME280 : public Usermod {
  private:
    // BME280 sensor settings
    BME280I2C::Settings settings(
      BME280::OSR_X16,
      BME280::OSR_X16,
      BME280::OSR_X16,
      BME280::Mode_Forced,
      BME280::StandbyTime_1000ms,
      BME280::Filter_Off,
      BME280::SpiEnable_False,
      BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
    );

    BME280I2C bme(settings);
};

IntelliSense illustrates what I mean: https://i.stack.imgur.com/14ql5.png

Can I do this? If so, what am I doing wrong?

1 Answers1

0

Yes it is possible to default initialize members, although in the C++ standard in-class initialization can only use = or {}. This is better answered here.

Using parentheses is confusing since it looks like a function declaration. IntelliSense is most likely complaining about error types since it thinks you are declaring a function with invalid types and a return type of BME280I2C::Settings (assuming the setting values you provided are values and not types).

The initilization could look like this assuming there exists a non-explicit constructor (notice the change from () to {})

#include <BME280I2C.h>

class UsermodBME280 : public Usermod {
  private:
    // BME280 sensor settings
    BME280I2C::Settings settings{
      BME280::OSR_X16,
      BME280::OSR_X16,
      BME280::OSR_X16,
      BME280::Mode_Forced,
      BME280::StandbyTime_1000ms,
      BME280::Filter_Off,
      BME280::SpiEnable_False,
      BME280I2C::I2CAddr_0x76 // I2C address. I2C specific.
    };

    BME280I2C bme{settings};
};
winidis
  • 140
  • 1
  • 7
  • Thank you very much, I've read through the post you linked as well and had no idea it was a Thing™, even been named the Most Vexing Parse and it makes complete sense to me now that you've pointed it out. – xBelladonna Oct 11 '20 at 13:21