不允许使用C++数据成员初始值设定项

use*_*915 10 c++ arrays

我对C++很陌生,所以请耐心等待.我想用静态数组创建一个类,并从main访问这个数组.这是我想用C#做的事情.

   namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class a = new Class();
                Console.WriteLine(a.arr[1]);

            }
        }
    }

    =====================

    namespace ConsoleApplication1
    {
        class Class
        {       
            public static string[] s_strHands = new string[]{"one","two","three"};
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

// justfoolin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class Class {

  public:
      static string arr[3] = {"one", "two", "three"};
};


int _tmain(int argc, _TCHAR* argv[])
{
    Class x;
    cout << x.arr[2] << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但我得到了:IntelliSense:不允许使用数据成员初始化程序

cdh*_*wie 16

您需要稍后执行初始化; 你不能在类定义中初始化类成员.(如果可以的话,那么在头文件中定义的类将导致每个翻译单元定义他们自己的成员副本,使链接器变得混乱以进行排序.)

class Class {
  public:
      static string arr[];
};

string Class::arr[3] = {"one", "two", "three"};
Run Code Online (Sandbox Code Playgroud)

类定义定义了接口,该接口实现分开.