は、以下のコードを検討:目的は持っているだろうは静的定数STDに可変長テンプレートリストから整数値を割り当て::アレイメンバ
MyClass<1,5,7,9> myClass;
:としてクラスを使用その後
template<unsigned... IDs>
class MyClass{
public:
static const std::array<unsigned, sizeof...(IDs)> ids { IDs... };
PinIDs() = default;
};
をサイズが4のids
で、値はそれぞれ(1,5,7,9)です。
このタイプのオブジェクトは使用できますか、静的修飾子を削除する必要がありますか?そうでない場合、静的修飾子でどのように記述しますか?オブジェクトはデフォルトで構成可能である必要があります。
EDIT:
私はアップル初のソリューションを試してみましたが、win7の 上のMSのVisual Studio 2017 CEと私は、このコンパイラエラーました:このような完全なオリジナルのソースで
1>------ Build started: Project: PracticeMath, Configuration: Debug Win32 ------ 1>stdafx.cpp 1>PracticeMath.cpp 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2988: unrecognizable template declaration/definition 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '<' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2059: syntax error: '<' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2039: 'ids': is not a member of '`global namespace'' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2143: syntax error: missing ';' before '{' 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(33): error C2447: '{': missing function header (old-style formal list?) 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(38): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(39): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(40): error C2065: 'myId': undeclared identifier 1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(44): error C2065: 'c': undeclared identifier 1>Done building project "PracticeMath.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
を:
#include <iostream>
#include <array>
template<unsigned... IDs>
class PinIDs{
public:
static const std::array<unsigned, sizeof...(IDs)> ids;
PinIDs() = default;
const unsigned& operator[](unsigned idx) const {
return ids[idx];
}
};
template<unsigned... IDs>
const std::array<unsigned, sizeof...(IDs)> PinIDs<IDs...>::ids { IDs... };
int main() {
PinIDs<4, 17, 19> myId;
std::cout << myId[0] << " ";
std::cout << myId[1] << " ";
std::cout << myId[2] << " ";
std::cout << "\nPress any key and enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
私はアップルの1st method
を適用しようとしたときに私の溶液中のクラスの実際の名前とは対照的に、私は誤ってMyClass
を混同しているという事実育てるストーリーテラーのおかげ - プロジェクトを。一度私はそれがコンパイルして、ビルドし、期待通りに実行することを訂正しました。
試しましたか? – StoryTeller
はい。しかし、問題を抱えていました... 'template'構文ビジネスか、単に' static'を使用しているのか分かりません... –
"問題がある"場合は、エラーを貼り付けてください –