[英]define constants in a struct
我正在嘗試創建一個包含一些常量的結構,如下所示:
#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
};
};
但是,我無法訪問這個 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);
這將使編譯器返回以下錯誤:
error: invalid use of non-static data member 'mute'
所以,我試着像這樣制作常量 static:
const static AudioObjectPropertyAddress volume = { ...
但是現在,我得到了一個不同的錯誤:
error: in-class initializer for static data member of type 'const AudioObjectPropertyAddress' requires 'constexpr' specifier
我嘗試的最后一件事是將const static
更改為static constexpr
,但是同樣,我無法訪問常量。 每次我嘗試訪問它們時,編譯器都會顯示此錯誤:
Undefined symbols for architecture x86_64:
"properties::mute", referenced from:
_main in main-fefb9f.o
ld: symbol(s) not found for architecture x86_64
我不太確定這里發生了什么,我嘗試將我的結構轉換為 class,但我最終得到了相同的Undefined symbols
錯誤。 我知道我可以將這兩個常量定義為全局變量,但我認為將它們放入結構/類中會使代碼看起來“更好”或更有條理。 有人可以解釋這里出了什么問題並提供可能的解決方案嗎?
為什么不只是 properties.volume 和 properties.mute 或者使用命名空間,否則......
namespace properties
{
//Volume control
const AudioObjectPropertyAddress volume = {
kAudioDevicePropertyVolumeScalar, //mSelector
kAudioDevicePropertyScopeOutput, //mScope
0 //mElement
};
//Mute control
const AudioObjectPropertyAddress mute = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
0
};
};
struct properties { //Volume control const AudioObjectPropertyAddress volume = { kAudioDevicePropertyVolumeScalar, //mSelector kAudioDevicePropertyScopeOutput, //mScope 0 //mElement }; };
有效,但volume
不是 static 成員,因此使用需要一個實例:
setProperty(1, properties{}.volume);
使用static const
,它將是:
struct properties { //Volume control static const AudioObjectPropertyAddress volume; }; const properties::AudioObjectPropertyAddress volume { kAudioDevicePropertyVolumeScalar, //mSelector kAudioDevicePropertyScopeOutput, //mScope 0 //mElement };
並且用法可能是預期的:
setProperty(1, properties::volume);
在 C++11/C++14 中使用static constexpr
:
struct properties { //Volume control static constexpr AudioObjectPropertyAddress volume{ kAudioDevicePropertyVolumeScalar, //mSelector kAudioDevicePropertyScopeOutput, //mScope 0 //mElement }; }; const properties::AudioObjectPropertyAddress volume; // Definition when ODR-used
自 C++17 以來,使用static /*inline*/ constexpr
:
struct properties { //Volume control static /*inline*/ constexpr AudioObjectPropertyAddress volume{ kAudioDevicePropertyVolumeScalar, //mSelector kAudioDevicePropertyScopeOutput, //mScope 0 //mElement }; };
以下是您可以嘗試的事項列表:
你可以......重新設計你的模板/類型。 結構與命名空間。 在沒有看到整個項目的情況下,很難在特定方向上進行指導。 由您決定是否需要使用結構、枚舉、命名空間、class 來封裝您的代碼。
至於 const static 聲明。 這是一篇描述 static class 成員聲明的有效方法的帖子。 請密切注意您的 c++ 版本。
“在 C++11 之前,只有 static 整型或枚舉類型的 const 數據成員可以在 class 定義中具有初始值設定項。”
最后,static constexpr 未定義。 您的 static var 是否在與您調用它的位置相同的頭文件/源文件中? (可能不是,因為錯誤顯示來自 main 的調用)。 Static 變量對其他文件不可見。
如果你想修改 static 變量,你可以使用來自 class function 的公共獲取/設置。
假設“靜音”是單個 uint8_t 值。 考慮使用一個命名空間,所有 static 數據聲明為:static const uint8_t mute。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.