Error C2864 Visual Studio
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 c2864 only static const integral data members can be initialized within a class Learn more about Stack Overflow the company Business Learn more about hiring developers or
A Static Data Member With An In-class Initializer Must Have Non-volatile Const Integral Type
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow c++ integral types Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C2864 compiler error up vote 2 down vote favorite c++ static const double 1 I am making some game and I get following error : class Apple:public Fruit{ public: bool isAppleOK = false; Apple(int amount, int pHValue) { amount = amount; pHValue= pHValue; } ~Apple() { } /*code trimmed*/ error C2864: 'Apple::isAppleOK ' : only static const integral data members can be initialized within a class What am I missing here? c++ visual-studio-2010 share|improve this question asked Jul 4 '12
Visual Studio 2010 C++11
at 21:50 Takarakaka 1142314 3 Note that, and Luchien hinted at this, the syntax you used is perfectly valid in C++11. It just fills in for whenever an initialization of isAppleOK is required. Just something to keep in mind. See this section of Bjarne's C++11 FAQ for more details. –chris Jul 4 '12 at 21:57 add a comment| 3 Answers 3 active oldest votes up vote 9 down vote accepted This is not the way you initialize member variables in C++. You need to set the value in the init list of the constructor: Apple(int amount, int pHValue) : isAppleOK(false) { amount = amount; pHValue= pHValue; } You can also move the initialization of your other variables into the initialization list: Apple(int amt, int pHv) : isAppleOK(false) , amount(amt) , pHValue(pHv) { } share|improve this answer answered Jul 4 '12 at 21:52 dasblinkenlight 455k39492842 OK, I am new to c++, does that mean that is the way to initalize member variables for inherited classes? –Takarakaka Jul 4 '12 at 21:57 1 @Takarakaka, You should always initialize your class members like this. They get initialized before the body anyway, so you might as well be the one
here for a quick overview of the site Help Center Detailed answers to any questions you might have visual studio c++11 Meta Discuss the workings and policies of this site About Us c++ constexpr Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just http://stackoverflow.com/questions/11335765/c2864-compiler-error like you, helping each other. Join them; it only takes a minute: Sign up Resolving “only static const integral data members can be initialized within a class” compilation error up vote 6 down vote favorite The following for creating a Global Object is resulting in compilation errors. #include "stdafx.h" #include
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 http://stackoverflow.com/questions/17566575/how-to-initialize-a-static-const-float-in-a-c-class-in-visual-studio Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs http://stackoverflow.com/questions/1907214/why-are-inlined-static-consts-not-allowed-except-ints Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack 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 initialize a static const float in a C++ class in Visual Studio up vote 3 down vote favorite 1 I have visual studio a code like this: class MyClass { private: static const int intvalue= 50; static const float floatvalue = 0.07f; }; in Visual studio 2010 and I am getting this error: Myclasses.h(86): error C2864: 'MyClass::floatvalue : only static const integral data members can be initialized within a class So how to initialize a static constant float in c++? If I use constructor, every time that an object of this class is created, the variable is initialized error c2864 visual which is not good. apparently the code is compiled with GCC on Linux. c++ visual-studio-2010 share|improve this question edited Jul 10 '13 at 9:55 Daniel Daranas 17.8k64583 asked Jul 10 '13 at 9:06 mans 3,8031262134 It doesn't compile on GCC for me, but gives a very nice pointer: error: ‘constexpr’ needed for in-class initialization of static data member ‘const float MyClass::floatvalue’ of non-integral type. Of course, considering that constexpr isn't even in VS2012... –chris Jul 10 '13 at 9:08 possible duplicate of C++ initialize static variables in class? –quetzalcoatl Jul 10 '13 at 9:09 add a comment| 3 Answers 3 active oldest votes up vote 10 down vote accepted MyClass.h class MyClass { private: static const int intvalue = 50; // can provide a value here (integral constant) static const float floatvalue; // canNOT provide a value here (not integral) }; MyClass.cpp const int MyClass::intvalue; // no value (already provided in header) const float MyClass::floatvalue = 0.07f; // value provided HERE Also, concerning apparently the code is compiled with GCC on Linux. This is due to an extension. Try with flags like -std=c++98 (or -std=c++03, or -std=c++11 if your version is recent enough) and -pedantic and you will (correctly) get an error. share|improve this answer edited Jul 10 '13 at 9:22 answered Jul 10 '13 at 9:09 gx_
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 Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why are “inlined” static consts not allowed, except ints? up vote 4 down vote favorite 1 Possible Duplicate http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class struct Example { static const int One = 1000; // Legal static const short Two = 2000; // Illegal static const float Three = 2000.0f; // Illegal static const double Four = 3000.0; // Illegal static const string Five = "Hello"; // Illegal }; Is there any reason for which #2, #3, #4 and #5 are illegal? I think I know the reason for #5: the compiler needs a "real" string object (since it's not a built in type) and cannot mindlessy replace Five with "Hello" as if it was #define Five "Hello". But if that's the case, can't the compiler leave an hint in the .obj files and tell the linker to automatically create one instance of string Five somewhere? For #3 and #4 and especially #2 (lol!)... I can't really see any possible reason! Floats and doubles are built-in types, just as int is! And short is just a (possibly) shorter integer. EDIT: I'm using Visual Studio 2008 to compile it. I thought all compilers behaved the same in this case, but apparently g++ compiles that fine (except #5). The errors VS gives for that snippets are: error C2864: 'Example::Two' : only static const integral data members can be initialized within a class error C2864: 'Example::Three' : only static const integral data members can be initialized within a class error C2864: 'Example::Four' : only static const integral data members can be initialized within a class error C2864: 'Example::Five' : only static const integral data members can be initialized within a class c++ static initialization class-members share|improve this question edited Jan 21 '10 at 1:27 asked Dec 15 '09 at 12:47 Andreas Bonini 23.1k19100140 have you tried it on different compilers ? If so which one's ? and what where the error messages ? –Hassan Syed Dec 15 '09 at 12:50 What compiler are you using? Compiles fine for me with G++ (except the string). –schnaader Dec 15 '09 at 12:51 VS2008 (edited the main qu