Enum in header showing as undefined in main.cpp

Hello! I’ve dabbled with arduino for a while and decided to write a library for the BMP280 to learn more.

I am however having some issues with my enums (first time using them).
I am using them to define some types, to prevent arbitrary values being passed to the chips registers.

Anyhow.

I have the enums in my header (pasted below), and they work fine in the BMP280i2c.h and BMP280i2c.cpp, but in my main.cpp they show as undefined.
The actual constructor and other functions are recognized just fine though, eg. if i write the following:

bmp.Init();
bmp.Config(STANDBY_125MS,IIR_FILTER_OFF);

bmp.Init(); passes just fine, bmp.Config() in itself passes just fine, but the standby and filter are both flagged as undefined.

#ifndef BMP280i2c_h
#define BMP280i2c_h
 
#include <Arduino.h>
#include <hardwareSerial.h>
#include <Wire.h>

#define BMP280Addr    (0x76)

class BMP280i2c
{
public:
	enum standbyPeriod {
	  STANDBY_0_5MS   =   0,
	  STANDBY_62_5MS  =   1,
	  STANDBY_125MS   =   2,
	  STANDBY_250MS   =   3,
	  STANDBY_500MS   =   4,
	  STANDBY_1000MS  =   5,
	  STANDBY_2000MS  =   6,
	  STANDBY_4000MS  =   7
	};

	enum filterCoeff {
	  IIR_FILTER_OFF  =   0,
	  IIR_FILTER_X2   =   1,
	  IIR_FILTER_X4   =   2,
	  IIR_FILTER_X8   =   3,
	  IIR_FILTER_X16  =   4
	};

	enum commType {
	  I2C             =   0,
	  SPI             =   1
	};

	enum powerMode {
	  BMP_SLEEP       =   0,
	  BMP_FORCED      =   1,
	  BMP_NORMAL      =   3
	};

	enum Sampling {
	  SAMPLE_SKIP =   0,
	  SAMPLE_X1   =   1,
	  SAMPLE_X2   =   2,
	  SAMPLE_X4   =   3,
	  SAMPLE_X8   =   4,
	  SAMPLE_X16  =   5
	};
	
	BMP280i2c();
	bool Init();
	void PrintDetails();
	bool SetMeas(powerMode pwr, Sampling pressSamp, Sampling tempSamp);
	bool Config(standbyPeriod stbyDur, filterCoeff filter);
	uint32_t GetPressurePA();
	float GetPressureHPA();
	float GetAltitude();
	float GetTemperature();
	bool Confirm();
	
private:
//Structure for storing calibration data
struct BMP280_Calibration_Data{
  unsigned short dig_T1;
  short dig_T2;
  short dig_T3;
  unsigned short dig_P1;
  short dig_P2;
  short dig_P3;
  short dig_P4;
  short dig_P5;
  short dig_P6;
  short dig_P7;
  short dig_P8;
  short dig_P9;
} bmpCalib;

//BMP280 Register Addresses
enum {
  BMP280Reg_dig_T1_LSB              = 0x88,
  BMP280Reg_dig_T1_MSB              = 0x89,
  BMP280Reg_dig_T2_LSB              = 0x8A,
  BMP280Reg_dig_T2_MSB              = 0x8B,
  BMP280Reg_dig_T3_LSB              = 0x8C,
  BMP280Reg_dig_T3_MSB              = 0x8D,  
  BMP280Reg_dig_P1_LSB              = 0x8E,
  BMP280Reg_dig_P1_MSB              = 0x8F,  
  BMP280Reg_dig_P2_LSB              = 0x90,
  BMP280Reg_dig_P2_MSB              = 0x91,  
  BMP280Reg_dig_P3_LSB              = 0x92,
  BMP280Reg_dig_P3_MSB              = 0x93,
  BMP280Reg_dig_P4_LSB              = 0x94,
  BMP280Reg_dig_P4_MSB              = 0x95,  
  BMP280Reg_dig_P5_LSB              = 0x96,
  BMP280Reg_dig_P5_MSB              = 0x97,
  BMP280Reg_dig_P6_LSB              = 0x98,
  BMP280Reg_dig_P6_MSB              = 0x99,  
  BMP280Reg_dig_P7_LSB              = 0x9A,
  BMP280Reg_dig_P7_MSB              = 0x9B,
  BMP280Reg_dig_P8_LSB              = 0x9C,
  BMP280Reg_dig_P8_MSB              = 0x9D,
  BMP280Reg_dig_P9_LSB              = 0x9E,
  BMP280Reg_dig_P9_MSB              = 0x9F,  
  BMP280Reg_ID                      = 0xD0,
  BMP280Reg_RESET                   = 0xE0,
  BMP280Reg_CTRL_MEAS               = 0xF4,
  BMP280Reg_CONFIG                  = 0xF5,
  BMP280Reg_PRESSURE_MSB            = 0xF7,
  BMP280Reg_PRESSURE_LSB            = 0xF8,
  BMP280Reg_PRESSURE_XLSB           = 0xF9,  
  BMP280Reg_TEMP_MSB                = 0xFA,
  BMP280Reg_TEMP_LSB                = 0xFB,
  BMP280Reg_TEMP_XLSB               = 0xFC
};

	uint8_t pressureOversampling;
	uint8_t temperatureOversampling;
	int32_t t_fine;
	void printByte(uint8_t byte, bool newLine);
	void GetCalibration();
	uint8_t readReg(uint8_t reg);
};
#endif

Can you give a clearer explanation of the exact file structure you are using?
Are you talking about a “header”, another header (BMP280i2c.h) and two source files (BMP280i2c.cpp and main.cpp)?
The prototype of the shown class is in the “header”? Where are the actual implementations of the classes methods? In which file do you #include what?

I can try!

BMP280i2c.h and BMP280i2c.cpp is my library, eg. Arduino library, located in its own folder, in the project folder.

The prototypes are in the BMP280i2c.h and implementations BMP280i2c.cpp

in my main.cpp i include:
<Wire.h>
<hardwareSerial.h>
<Arduino.h>
“BMP280i2c.h”

in BMP280i2c.cpp i only include “BMP280i2c.h”
In BMP280i2c.h i include:
<Wire.h>
<hardwareSerial.h>
<Arduino.h>

To be clear; The functions are functioning, i can use the functions that doesn’t require arguments, and the functions that require arguement “know” that they’re looking for the custom types, it’s only the enums from BMP280i2c.h that shows as undefined when being used in main.cpp, the enums work fine in BMP280i2c.h and BMP280i2c.cpp.

Screenshot_2

Looks good to me… it’s a little wierd that you can’t use the enums specifically. Your enums are a field (part) of your class which is something I have never seen anyone do before :smiley:
Do you by any chance try to use the enums as if they were defined outside of a class?
Maybe you should not make your enums part of the class. Define your enums before the class (i.e. before class BMP280i2c {...}) in your BMP280i2c.h header. This way you could use them in- and outside of the class definitions and instances and in your library and main source-file.

2 Likes

That’s the ticket!
enums works just fine now that they’re outside the class, alternatively found that i could use them while they’re inside the class (but as you said, why would i?) by adding "BMP280i2c:: " in front of them in the main file, which makes sense since they were inside the class…

I learned something, thank you Thomseen :slight_smile: