Error C2864 Static Const Char
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more
Static Char C++
about Stack Overflow the company Business Learn more about hiring developers or posting static const char c ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Static Const Char Vs Const Char
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to declare a static const char* in your header file? c++ initialize static const char array up vote 41 down vote favorite 4 I'd like to define a constant char* in my header file for my .cpp file to use. So I've tried this: private: static const char *SOMETHING = "sommething"; Which brings me with the following compiler error: error C2864: 'SomeClass::SOMETHING' : only static const integral data members can be initialized within a class I'm new to C++. What is going on here? Why c++ static char array is this illegal? And how can you do it alternatively? c++ constants share|improve this question asked Oct 28 '09 at 18:24 Mark 1,84872647 1 You should use "static const char* const SOMETHING" instead, unless you really want to be able to reassign SOMETHING to point at something else at runtime. –bk1e Oct 28 '09 at 18:54 add a comment| 9 Answers 9 active oldest votes up vote 49 down vote accepted You need to define static variables in a translation unit, unless they are of integral types. In your header: private: static const char *SOMETHING; static const int MyInt = 8; // would be ok In the .cpp file: const char *YourClass::SOMETHING = "something"; C++ standard, 9.4.2/4: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer. share|improve this answer edited Jul 25 '14 at 7:32 answered Oct 28 '09 at 18:27 K
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack
Static Constexpr Const Char*
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow c++ const char array initialization is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to initialize a
C++ Const Char Initialization
static const member in C++? up vote 35 down vote favorite 13 Is it possible to initialize a static const value outside of the constructor? Can it be initialized at the same place where member declarations are found? class A { private: static http://stackoverflow.com/questions/1639154/how-to-declare-a-static-const-char-in-your-header-file const int a = 4; /*...*/ }; c++ static share|improve this question edited Oct 30 '12 at 1:06 jogojapan 39.7k76088 asked Aug 20 '10 at 13:06 anarhikos 3291818 8 Yes, what you have works (but only for integral types). –UncleBens Aug 20 '10 at 13:07 2 Just wanted to add that static has nothing to do with constructors since static members are not specific to a given instance and exist outside of it. –ereOn Aug 20 '10 at 13:10 In fact, you http://stackoverflow.com/questions/3531060/how-to-initialize-a-static-const-member-in-c must initialize const statics outside a class constructor. Otherwise it wouldn't be a const static. DUCY? –John Dibling Aug 20 '10 at 14:10 add a comment| 5 Answers 5 active oldest votes up vote 42 down vote accepted YES you can but only for int types. If you want your static member to be any other type, you'll have to define it somewhere in a cpp file. class A{ private: static const int a = 4; // valid static const std::string t ; // can't be initialized here ... ... }; // in a cpp file where the static variable will exist const std::string A::t = "this way it works"; Also, note that this rule have been removed in C++11, now (with a compiler providing the feature) you can initialize what you want directly in the class member declaration. share|improve this answer edited Jan 28 '12 at 11:38 answered Aug 20 '10 at 13:12 Klaim 32.6k2190155 why static const double = value; is not working ? –anarhikos Aug 20 '10 at 13:21 2 @anarhikos - the in-class initialization only works for integral types. double is not an integral type. –Brian Neal Aug 20 '10 at 13:31 1 @anrhikos: That's why you shouldn't define inside the class. You should define outside the class as a matter of practice (see my answer) –user195488 Aug 20 '10 at 13:35 What if I don't want to assign a value? What if I just want the default constructor to run? I get errors about re-declaring the va
file? I'd like to define a constant char* in my header file for http://kechengpuzi.com/q/s1639154/a/0 my .cpp file to use. So I've tried this: private: static const char *SOMETHING = "sommething"; Which brings me with the following compiler error: error C2864: 'SomeClass::SOMETHING' : only static const integral data members can be initialized within a const char class I'm new to C++. What is going on here? Why is this illegal? And how can you do it alternatively?
| 2014-07-25 You need to define static variables in a translation unit, unless they are of integral static const char types. In your header: private: static const char *SOMETHING; static const int MyInt = 8; // would be ok In the .cpp file: const char *YourClass::SOMETHING = "something"; C++ standard, 9.4.2/4: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer. SUPPORT US 扫一扫关注微信公众号 课程铺子, 可能是个更懂你的课程搜索引擎。 © 2016 课程铺子 kechengpuzi@gmail.com 课程铺子上线283天 课程铺子,每天陪你进步一点 冀ICP备16011938号-2