#### Archived

This topic is now archived and is closed to further replies.

# compiler error: expected constant expression (to do with static const class members)

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

## Recommended Posts

Hi, I''m getting a compile error with VC++ with (effectively) the following code:


//Blah.h

class Blah
{
private:
static const int    m_BufferSize;
static char         m_Buffer[m_BufferSize];
};

//Blah.cpp

const int    Blah::m_BufferSize = 10;
char         Blah::m_Buffer[Blah::m_BufferSize];

The VC++ compiler gives the following error: : error C2057: expected constant expression Is there anything wrong with the above or is the compiler wrong? (I''m reasonably sure that that would compile ok using GCC (maybe not though )). I tried changing the m_BufferSize declaration (in the header) to: static const int m_BufferSize = 10; but got the errors: : error C2258: illegal pure syntax, must be ''= 0'' : error C2252: ''m_BufferSize'' : pure specifier can only be specified for functions : error C2065: ''m_BufferSize'' : undeclared identifier I''m also fairly certain that should be ok. Anyone know what''s wrong? Thanks.

##### Share on other sites
test.cpp:7: storage size of Blah::m_Buffer'' isn''t constant
test.cpp:12: conflicting types for char Blah::m_Buffer[10]''
test.cpp:7: previous declaration as char Blah::m_Buffer[((Blah::m_BufferSize - 1) + 1)]''

This is what i get when i try to compile it with gcc (the last error is totally weird)

The simplest solution would probably be to #define BUFFER_SIZE 10

##### Share on other sites
there are actually two ways to do it.

foo{ static const int size_=10;};bar{ static const int size_;};const int bar::size_=20;`

Both would compile with gcc and I am not sure about versions of vc++

##### Share on other sites
Synapse: That''s what I thought, and it would appear that VC++ won''t compile either :-/

Thanks

##### Share on other sites
hrm, what version? I would think .net would and everett will..

I think vc++6 you had to use the enum workaround

iirc

class foo
{
enum { size_=10 };
char array_[size_];
};

or something to that effect

##### Share on other sites
I am indeed using VC++ 6.
I refuse to make my code all *dirty* with a fix like that

I''ve changed my design now anyway so I don''t need to do that.
Thanks though.

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997734
×