Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ data member initializer is not allowed

Tags:

c++

arrays

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#.

   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

like image 884
user1435915 Avatar asked Jun 07 '12 16:06

user1435915


People also ask

How do you initialize a data member in C++?

Static Data Member Initialization in C++ We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator, then the variable name. Now we can assign some value.

How to initialize non-static variable in c++?

Non-static member initialization (also called in-class member initializers) provides default values for your member variables that your constructors will use if the constructors do not provide initialization values for the members themselves (via the member initialization list).

Is a nonstatic data member?

Non-static data members are the variables that are declared in a member specification of a class. a non-static data member cannot have the same name as the name of the class if at least one user-declared constructor is present.

What is non-static member in c++?

final (C++11) [edit] A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)


2 Answers

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.

like image 86
cdhowie Avatar answered Oct 17 '22 22:10

cdhowie


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"}; 
like image 26
Patrick Avatar answered Oct 17 '22 23:10

Patrick