前提
C++/CLIのDLLを作成しています。
同DLL内の他クラス内から参照する定数クラス作成を試みているところとなります。
質問
宣言時に初期化まで行う連想配列std::mapを作成しようとしていますが、以下のエラーが発生します。
対処方法があれば、ご教授願えないでしょうか。
※目的自体は連想配列作成になるため、std::mapにこだわるわけではありません。
エラーコード:C2440
メッセージ:
'初期化中': 'initializer list' から 'std::map<System::String ^,System::Nullable<int> ^,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> *' に変換できません。
サンプルコード
C++
1ref class constantClass 2{ 3public: 4 static map<System::String^, System::Nullable<int>^>* dic = 5 { 6 { gcnew System::String("AAA"), gcnew System::Nullable<int>(1) }, 7 { gcnew System::String("BBB"), gcnew System::Nullable<int>(2) }, 8 { gcnew System::String("CCC"), gcnew System::Nullable<int>(3) }, 9 { gcnew System::String("DDD"), gcnew System::Nullable<int>(4) }, 10 { gcnew System::String("EEE"), gcnew System::Nullable<int>(5) }, 11 { gcnew System::String("FFF"), gcnew System::Nullable<int>(6) }, 12 { gcnew System::String("GGG"), gcnew System::Nullable<int>(7) } 13 }; 14};
試してみたこと
①System::Collections::Generic::Dictionary型に型変更
⇒エラー「配列初期化子はC++/CLI配列型の配列化にのみ使用できます」が発生。
C++
1 static Dictionary<System::String^, System::Nullable<int>> dic = gcnew Dictionary<System::String^,System::Nullable<int>>() 2 { 3 { gcnew System::String("AAA"), gcnew System::Nullable<int>(1) }, 4 { gcnew System::String("BBB"), gcnew System::Nullable<int>(2) }, 5 { gcnew System::String("CCC"), gcnew System::Nullable<int>(3) }, 6 { gcnew System::String("DDD"), gcnew System::Nullable<int>(4) }, 7 { gcnew System::String("EEE"), gcnew System::Nullable<int>(5) }, 8 { gcnew System::String("FFF"), gcnew System::Nullable<int>(6) }, 9 { gcnew System::String("GGG"), gcnew System::Nullable<int>(7) } 10 };
開発環境
ソフトウェア:Visual Studio 2015 Professional
.NET:バージョン4.6
回答1件
あなたの回答
tips
プレビュー