1

what's wrong with this syntax? sorry for the newbie question.

source:

Level::Level()
{

    NintyDegreeDirections[4] =  
    { 
        (float)(2*(Math.PI)), 
        (float)(3*(Math.PI)/2), 
        (float)Math.PI, 
        (float)Math.PI/2 
    }

...rest of class

header:

//all necessary includes

class Level
{
private:

    static const float NintyDegreeDirections[4];

...rest of header

how do I have an array as a class member? I'm converting from C#

2
  • You'd get more and better answers if your code was (a) complete and compilable, that is, no snippets like "all necessary includes" and (b) you would state the actual errors you got in compiling. Commented Oct 16, 2011 at 9:52
  • 1
    static will make it a class member, which means that you don't initialize it on a per-instance basis, but once, for the whole class. You trying to initialize it in the constructor suggests you might want an instance member instead of a class member, but I'm not sure. Commented Oct 16, 2011 at 9:52

1 Answer 1

3

If you want to initialize your static class member in the source file, you need to do it outside of any function body. You also need to retain all of the type information in the definition. E.g.

// In level.cpp, at namespace (global) scope:
const float Level::NintyDegreeDirections[4] =  
{ 
    (float)(2*(Math.PI)), 
    (float)(3*(Math.PI)/2), 
    (float)Math.PI, 
    (float)Math.PI/2 
};

(This assumes that you have defined a Math namespace or class with an appropriate member PI. Math isn't native to C++.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.