Jump to content
  • Advertisement
Sign in to follow this  
snooty

static const double PI = 3.1415

This topic is 4364 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
well, you could try this:



const struct
{
operator const double() const { return 3.1415; }
} PI;





Although, PI is already defined in <cmath>

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

class Math
{
public:
static const double PI;
};

static const double Math::PI = 3.1415;


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
The correct thing to do would be to #define _USE_MATH_DEFINES before including cmath, and then use M_PI

Share this post


Link to post
Share on other sites
Quote:
Original post by ordered_disorder
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?


Something too clever for my own good, and altogether useless, really.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
<nitpick>
It should be 3.1416, or preferably something more precise. I use the value of pi from windows calculator (3.1415926535897932384626433832795)
</nitpick>

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!