3
class ARouter {
    enum directions {north, neast, east, seast, south, swest, west, nwest};
    static directions gon[] = {north, neast, nwest, east, west, seast, swest, south};
};

Hi, does anyone know what is the matter with the above code?

I am getting 2 errors for the second line from VC++2008Ex:

error C2059: syntax error : '{'

error C2334: unexpected token(s) preceding '{'; skipping apparent function body

7
  • 1
    There is nothing wrong with those lines. You probably have a syntax error further up the file, which is causing the compiler to become confused. Or perhaps you're defining the variable somewhere invalid, like inside a class body. Commented Aug 7, 2011 at 19:19
  • Post more code, it compiles on my end. Commented Aug 7, 2011 at 19:20
  • Nothing wrong with it AFAIK; your code works fine in g++ 4. Commented Aug 7, 2011 at 19:21
  • You sure? I just bracketed it off into a little class for completeness. I haven't really changed anything else to break it. I'll take the lines out and try again. brb. Commented Aug 7, 2011 at 19:22
  • Back. Nope, those lines are definitely breaking it. Commented Aug 7, 2011 at 19:23

1 Answer 1

5

You cannot define a variable inside a class like that.

It needs to be something like:

class ARouter {
    enum directions {north, neast, east, seast, south, swest, west, nwest};
    static directions gon[];
};

ARouter::directions ARouter::gon[] = {north, neast, nwest, east, west, seast, swest, south};

The declaration goes in the class body; the definition lives outside. Note that you'd typically put the class body in a header, and the definition in a source file.

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

1 Comment

The pseudonym "class variable" mislead me in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.