PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Funky static const errors.. anyone?

I've been programming my app in VS.NET, and finally decided to compile in eVC4. I got some errors which I find odd;

Code (declared in CGameWindow class):
static const DWORD DEFAULT_HEIGHT = 100;

Errors:
c:\source\generic\window.h(11) : error C2258: illegal pure syntax, must be '= 0'
c:\source\generic\window.h(11) : error C2252: 'DEFAULT_HEIGHT' : pure specifier can only be specified for functions
...

I just don't get it. As far as I know it's perfectly legal C++ code. Am I going to have to use #define for my constants? I'd prefer not to, but I just don't know what's going on here.
In VC 6 (and thus EVC 3 and 4), you can't declare static const member variables. The usual work around is to use enums:

class MyClass
{
enum
{
MY_CONSTANT = 100;
};

};

But then you lose type safety.

If it's uneeded in the header, then just declare it in the .cpp as a "local" static variable.
Ah, okay. Well at least I know I wasn't going crazy. :) Thanks for the tip, enum seems like a slightly better alternative than #define (must.. not.. use.. globals).
Pejo Software - Per · 21 Jan 2004, 18:34 UTC
Thierry are you sure?

I think the following will work:

class CGameWindow
{
static const DWORD DEFAULT_HEIGHT;
};

In cpp file:
const DWORD CGameWindow::DEFAULT_HEIGHT=100;
I don't think it does, but if it works then it not a constant anymore. It's a real variable.

I am sure though that you can't declare and initialize a static const member variable in the header file (before .NET) so that it get used as a constant.
Static const integral types with an initializers, in the class definition (what most people call declaration but it isn't), are legal C++ but I believe not supported in these compilers.

Remember, in itself that's a variable declaration, and you still have to define it in the source file.

See Stroustrup's book for details.
Pejo Software - Per · 21 Jan 2004, 19:00 UTC
Initialization in the header file will not work but as I shown I really think initialization in the source file will work. Static member variables shall be initialized in the source file (at least this is what I was teached when I learned C++ 10 years ago).
Pejo Software - Per wrote:Initialization in the header file will not work but as I shown I really think initialization in the source file will work. Static member variables shall be initialized in the source file (at least this is what I was teached when I learned C++ 10 years ago).


A lot has changed in the last 10 years =) C++ wasn't standard back then!
Pejo Software - Per wrote:Thierry are you sure?

I think the following will work:

class CGameWindow
{
static const DWORD DEFAULT_HEIGHT;
};

In cpp file:
const DWORD CGameWindow::DEFAULT_HEIGHT=100;


That was the first thing I tried. It didn't work. It gave me a whole new set of errors; something about redefining the variable, and when I used another static const like this for array declaration, it said the array size was zero.
Pejo Software - Per · 21 Jan 2004, 19:16 UTC
I actually tried this and it compiled and when I ran it it worked as it should. If I tried to change the value in the code the compiler complained as it should of it being contant.
Hmm. Perhaps I made a typo when I was doing it.