こちら↓を参考に
static メンバ
[追記] ※入力の範囲チェックはしていません。
text
1usr ~/Project/test % ./a.out
20
3Joker
4usr ~/Project/test % ./a.out
52
62
7usr ~/Project/test % ./a.out
812
9Q
cpp
1usr ~/Project/test % cat t3.cpp
2#include <string>
3#include <iostream>
4
5using std::string;
6
7class Trump
8{
9 private:
10 static const string number_to_name[]; //C2864エラー
11 public:
12 string Translate(int i) const
13 {
14 return number_to_name[i];
15 }
16};
17
18const string Trump::number_to_name[] = {"Joker", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
19
20int main( )
21{
22 int pos = 0;
23 Trump tp;
24 //
25 std::cin >> pos;
26 std::cout << tp.Translate(pos) << std::endl;
27 //
28 return 0;
29}
別解
cpp
1#include <string>
2#include <iostream>
3
4using std::string;
5
6class Trump
7{
8 private:
9 static const string number_to_name[];
10 public:
11 const string & operator[](int i)const
12 {
13 return number_to_name[i];
14 }
15};
16
17const string Trump::number_to_name[] = {"Joker", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
18
19int main( )
20{
21 int pos = 0;
22 Trump tp;
23 //
24 std::cin >> pos;
25 std::cout << tp[pos] << std::endl;
26 //
27 return 0;
28}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。