Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ a member with an in-class initializer must be const

I am trying to create a static string in my class: (in my header file)

static string description = "foo"; 

but I'm getting this error:

IntelliSense: a member with an in-class initializer must be const 

if I change it to this:

static const string description = "foo"; 

I get this error instead:

IntelliSense: a member of type "const std::string" cannot have an in-class initializer 

What did I do wrong?

like image 590
Chin Avatar asked Nov 07 '12 17:11

Chin


People also ask

How do you initialize all type const data member during declaration?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.

Can constant be initialized?

A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type. Constant variables can be declared for any data types, such as int , double , char , or string .

What is static const in C++?

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.

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

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. The following code will illustrate the of static member initializing technique.


2 Answers

What you can do is declare the string in the header and initialize it in your .cpp.

in MyClass.h

#include <string> class MyClass {   static std::string foo; } 

in MyClass.cpp

#include "MyClass.h" std::string MyClass::foo = "bar" 
like image 79
Kevin MOLCARD Avatar answered Sep 17 '22 14:09

Kevin MOLCARD


Ignoring the specific error message the core issue is that you are trying to initialize a static member attribute in the declaration while in general that should be done in the definition.

// header struct test {   static std::string x; }; // single cpp std::string test::x = "foo"; 

Now back to the error messages. There is an exception in the C++03 standard that allows to provide an initializer to the declaration of a constant integral type, so that the value can be visible in all translation units that include the header and can thus be used as a constant expression:

// header struct test {    static const int size = 10; }; // some translation unit can do struct A {    int array[test::size]; }; 

If the value was defined in the definition of the variable, then the compiler could only use it in that single translation unit. It seems that your compiler is doing two tests, one for const-ness and a separate one for the integral part and thus two error messages.

Another thing that might affected that design in the compiler is that the C++11 standard allows for initializers in the declaration of non-static members of a class, which will then be used in the initializer list of each constructor that does not provide a value for that field:

struct test {    int a = 10;    int b = 5;    test() : a(5) // b(5) implicitly generated    {}  }; 

This is unrelated to your particular issue, as your member is static but it probably explains why the tests in the compiler were split as they are.

like image 24
David Rodríguez - dribeas Avatar answered Sep 20 '22 14:09

David Rodríguez - dribeas