static const double PI = 3.1415
Hi,
I tried the following:
class Math
{
public:
static const double PI = 3.1415;
};
But the compiler complains:
only static const integral data members can be initialized within a class
What is going on? Why can't double be initialized? Any solution?
Please help.
well, you could try this:
Although, PI is already defined in <cmath>
const struct{ operator const double() const { return 3.1415; }} PI;
Although, PI is already defined in <cmath>
Quote:Original post by daerid
well, you could try this:
*** Source Snippet Removed ***
Although, PI is already defined in <cmath>
I suck at c++. What are you trying to accomplish with that const struct?
You can assign a static const inside the class ... what you want is this:
in your .h:
class Math
{
public:
static const double PI;
};
Then in your .cpp:
static const double Math::PI = 3.1415;
Now i'm not 100% on that sytax its off the top of my head, but its something pretty close to that.
[Edit] Beaten :P
in your .h:
class Math
{
public:
static const double PI;
};
Then in your .cpp:
static const double Math::PI = 3.1415;
Now i'm not 100% on that sytax its off the top of my head, but its something pretty close to that.
[Edit] Beaten :P
The correct thing to do would be to #define _USE_MATH_DEFINES before including cmath, and then use M_PI
Quote:Original post by ordered_disorderQuote:Original post by daerid
well, you could try this:
*** Source Snippet Removed ***
Although, PI is already defined in <cmath>
I suck at c++. What are you trying to accomplish with that const struct?
Something too clever for my own good, and altogether useless, really.
Quote:Original post by snooty
But the compiler complains:
only static const integral data members can be initialized within a class
I believe integral, in this sense, means integers. That means no floating-point values (ie. no floats or doubles). I could be wrong, though. Usually I initialize my variables in the constructor.
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>
Quote:Original post by snooty
But the compiler complains:
only static const integral data members can be initialized within a class
It does what it says on the tin.
Quote:Original post by snooty
Why can't double be initialized?
Because the C++ ISO standard says so.
Quote:Original post by snooty
Any solution?
All static non-integral constants must be defined outside of class defintions, just as in kaysik's example.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement