0

Inside class, I would like to define some const strings.however the compiler reports error when I use

class A {
static const std::string s = "test" 
};

inside the class. How to do that? Do I have to define the const outside the class defination?

user705414
  • 20,472
  • 39
  • 112
  • 155
  • As numerous answers have already stated, you must define the value outside the class declaration (typically not in the header file). You can, however, give a value to a built-in type (so sub 'int' for 'std::string' in your example and it will work). – Gian Paolo May 17 '11 at 23:48

3 Answers3

4

I think you have to define it outside.

class A {
  static const std::string s;
};

const std::string A::s("test");
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • @Christian , we can define integer const inside, is that right? – user705414 May 17 '11 at 23:42
  • but for integer const you could also use enum. – Christian Rau May 17 '11 at 23:45
  • 1
    integer constants can be defined inside a class! – user258808 May 17 '11 at 23:48
  • static const int s = 10; is OK inside class, at least in VS2010. Wondering if this is c++ standard. – user705414 May 17 '11 at 23:50
  • 1
    @user705414: It is indeed. Static const integral types, including boolean but not including pointer, can be initialized inside the class. – Puppy May 17 '11 at 23:56
  • 1
    Currently the standard specifies that integral constants can be initialized inside class declaration. Read [this](http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class) for a good explanation why it is so. – user258808 May 17 '11 at 23:58
  • @user258808, @GMan: Note the need for careful use of vocabulary. They *can* be initialized inside the class, but such a declaration is *still not a definition*, and thus Boaz was correct to say they cannot be *defined* inside the class. Such static const integers live in a strange state where they can be used in contexts where a constant expression is required (e.g. array size), but if they're used in any other context then a definition is needed. See 3.2/2 and 9.4.2/2-4. C++0x changes the rules a bit, and introduces the term "odr-used". – Steve Jessop May 18 '11 at 00:48
  • @Steve: I'll give you that, but I doubt the nuance was intended. :) – GManNickG May 18 '11 at 05:00
0

Yes, it should be defined out side the class definition.

const std::string A::s = "test" ;

In C++0x initialization is allowed in the class definition itself (result 1) . But I don't why for std::string type it isn't allowed ( result 2 ).

Ahh .. from the error message it seems it is allowed only for integral data types.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

You initialize the static member outside the class (unlike C# where you can declare and initialize a member at the same place).

class A {
static const std::string s;
};

// initialize your static members outside of the class definition.
const std::string A::s = "Some text here";
C.J.
  • 15,637
  • 9
  • 61
  • 77
  • Not only should they be outside of the class, they should also be (generally speaking) only defined in a .cpp file and not in a .h, or you'll likely violate the [One Definition] Rule(http://en.wikipedia.org/wiki/One_Definition_Rule). – Boaz Yaniv May 17 '11 at 23:46
  • If I use #pragma once in the header, is that OK? – user705414 May 17 '11 at 23:52