lundi 27 septembre 2021

define constants in a struct

I'm trying to create a struct with some constants in it like this:

#include <CoreAudio/CoreAudio.h>
...
struct properties {
    //Volume control
    const AudioObjectPropertyAddress volume = {
        kAudioDevicePropertyVolumeScalar, //mSelector
        kAudioDevicePropertyScopeOutput, //mScope
        0 //mElement
    };
    //Mute control
    const AudioObjectPropertyAddress mute = { 
        kAudioDevicePropertyMute,
        kAudioDevicePropertyScopeOutput,
        0
    };
};

However, I cannot access the constants in this class;

//Function used to for example set volume level or set mute status
//bool setProperty(UInt32 data_to_write, AudioObjectPropertyAddress addr_to_write_to);
//Following line should mute the system audio
setProperty(1, properties::mute);

This will make the compiler return the following error:

error: invalid use of non-static data member 'mute'

So, I tried making the constants static like this:

const static AudioObjectPropertyAddress volume = { ...

But now, I get a different error:

error: in-class initializer for static data member of type 'const AudioObjectPropertyAddress' requires 'constexpr' specifier

The last thing I tried is changing const static to static constexpr, however again, I cannot access the constants. Every time I try to access them, the compiler show this error:

Undefined symbols for architecture x86_64:
  "properties::mute", referenced from:
      _main in main-fefb9f.o
ld: symbol(s) not found for architecture x86_64

I'm not really sure what's going on here, I tried converting my struct into a class, but I ended up getting the same Undefined symbols error. I know I could just define the two constants as global variables, but I thought putting them into a struct/class would make the code look "nicer" or just more organized. Could someone please explain what's wrong here and provide a possible solution?

Aucun commentaire:

Enregistrer un commentaire