不允许使用c++数据成员初始化器

时间:2022-09-30 21:22:06

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++非常熟悉。我想创建一个具有静态数组的类,并从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"};
        }
    }

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:数据成员初始化器是不允许的

4 个解决方案

#1


16  

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.

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

#2


2  

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

#3


1  

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.

类定义中只能初始化静态、整数类型的数据成员。您的静态数据成员是字符串类型的,因此不能对其进行内联初始化。

You must define arr outside 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的错误列表中出现的所有错误都是构建错误。值得注意的是,以“智能感知”开头的错误是智能感知已经检测到的错误。智能感知和编译器并不总是一致的。

#4


0  

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

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

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

#1


16  

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.

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

#2


2  

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

#3


1  

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.

类定义中只能初始化静态、整数类型的数据成员。您的静态数据成员是字符串类型的,因此不能对其进行内联初始化。

You must define arr outside 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的错误列表中出现的所有错误都是构建错误。值得注意的是,以“智能感知”开头的错误是智能感知已经检测到的错误。智能感知和编译器并不总是一致的。

#4


0  

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

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

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