enum vs define in class

Hi all,

I’ve spent a some time reading about this but without finding a reasonable solution.
I am hoping someone can shed some light.

I am trying to use a public enum within a class to make code outside of the header and cpp file easier to read. Moving the enum declaration to the main .ino makes it available in main, but seems to defeat the purpose of the having the header in the first place. Also, I believe that an enum declared in the main would not be accessible from the cpp file…

#define within the public class definition works the way I expect it to, but enum gives an error: identifier “xxxx” is undefined

header file:

// Config.h

#ifndef _CONFIG_h
#define _CONFIG_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "arduino.h"
#else
	#include "WProgram.h"
#endif



class Config {
  public:
    /// <configValues>

  

    // af mode ----------------------------------------------------------------------
   
    #define VALUE_DEFINED_IN_HEADER 3333

    enum AF_MODES {
      AF_AUTO,
      AF_MANUAL
      AF_DELAYED
    };
    AF_MODES AF_MODE = AF_AUTO;

  
};

#endif

and the main:

#include "Config.h" 

// Create instance of class
Config currentConfig;



// the setup function runs once when you press reset or power the board
void setup() {
  int i = VALUE_DEFINED_IN_HEADER;
  int j = currentConfig.AF_MODE;
  if (currentConfig.AF_MODE == AF_AUTO) { //identifier "AF_AUTO" is undefined
    // can't use the enum...
  }
}

// the loop function runs over and over again until power down or reset
void loop() {
  
}

Many thanks!

The enum is defined inside of the class, so you have to specify the class name when using it outside of the class:

if (currentConfig.AF_MODE == Config::AF_AUTO)

Never use preprocessor defines as enum values or constants. Use enums and const or constexpr variables. (C++ Core Guidelines)
As a general rule, only use preprocessor defines for conditionally enabling/disabling pieces of code, for example to support different platforms.

constexpr static int VALUE_DEFINED_IN_HEADER = 3333;

Pieter

if (currentConfig.AF_MODE == Config::AF_AUTO)

Also:

   enum AF_MODES {
      AF_AUTO,
      AF_MANUAL  // <------ MISSING COMMA
      AF_DELAYED
    };

PieterP:
The enum is defined inside of the class, so you have to specify the class name when using it outside of the class:

if (currentConfig.AF_MODE == Config::AF_AUTO)

Never use preprocessor defines as enum values or constants. Use enums and const or constexpr variables. (C++ Core Guidelines)
As a general rule, only use preprocessor defines for conditionally enabling/disabling pieces of code, for example to support different platforms.

constexpr static int VALUE_DEFINED_IN_HEADER = 3333;

Pieter

Ah - perfect. Now I understand.

I will read the guidelines to understand why not to use #define.

@gfvalvo - Yes, that missing comma typo was introduced by manually editing after pasting the code... sorry.