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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10936009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:38:06  来源:igfitidea点击:

C++ data member initializer is not allowed

c++arrays

提问by user1435915

I totally new to C++ so bear with me. I want to make a class with a static array, and access to this array from the main. Here is what i want to do in C#.

我对 C++ 完全陌生,所以请耐心等待。我想用静态数组创建一个类,并从主访问这个数组。这是我想在 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"};
        }
    }

Here is what i have tried:

这是我尝试过的:

// 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;
}

But i got: IntelliSense: data member initializer is not allowed

但是我得到了:IntelliSense:不允许使用数据成员初始值设定项

回答by cdhowie

You need to perform the initialization later; you cannot initialize class members within the class definition. (If you could, then classes defined in header files would cause each translation unit to define their own copy of the member, leaving the linker with a mess to sort out.)

您需要稍后进行初始化;您不能在类定义中初始化类成员。(如果可以,那么在头文件中定义的类将导致每个翻译单元定义他们自己的成员副本,让链接器整理起来一团糟。)

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

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

The class definition defines the interface, which is separate from the implementation.

类定义定义了与实现分开的接口

回答by Patrick

You must initialize static members outside your class, as if it would be a global variable, like this:

你必须在你的类之外初始化静态成员,就好像它是一个全局变量,像这样:

class Class { 

  public: 
      static string arr[3];
}; 

string Class::arr = {"one", "two", "three"}; 

回答by James McNellis

Only static, integer-type data members may be initialized in the class definition. Your static data member is of type string, so it cannot be initialized inline.

在类定义中只能初始化静态、整数类型的数据成员。您的静态数据成员的类型为string,因此无法内联初始化。

You must definearroutside of the class definition, and initialize it there. You should remove the initializer from the declaration and the following after your class:

你必须定义arr的类定义之外,并初始化它。您应该从声明中删除初始化程序,并在您的课程之后删除以下内容:

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

If your class is defined in a header file, its static data members should be defined in exactly one source (.cpp) file.

如果您的类是在头文件中定义的,那么它的静态数据成员应该在一个源 (.cpp) 文件中定义。

Also note that not all errors that appear in the Error List in Visual Studio are build errors. Notably, errors that begin with "IntelliSense:" are errors that IntelliSense has detected. IntelliSense and the compiler do not always agree.

另请注意,并非 Visual Studio 中的错误列表中出现的所有错误都是构建错误。值得注意的是,以“IntelliSense:”开头的错误是 IntelliSense 检测到的错误。IntelliSense 和编译器并不总是一致的。

回答by piokuc

You have to initialize your static member outside of the class declaration:

您必须在类声明之外初始化静态成员:

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