473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Declaring constants within the scope of a class

I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};
My GCC compiler (g++ version 2.95.2) is giving the following misleading
compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'
I suspect that initialization of static data members that are not of an
integral type is not allowed in C++, but the compiler is giving a wrong
error message. Kindly confirm/refute my observation.

Thanks,
Gus

Jul 23 '05 #1
5 2620

Generic Usenet Account wrote:
I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};
My GCC compiler (g++ version 2.95.2) is giving the following misleading compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'
I suspect that initialization of static data members that are not of an integral type is not allowed in C++, but the compiler is giving a wrong error message. Kindly confirm/refute my observation.

Thanks,
Gus


I'm not sure if this will work, but try:

static const char* const yy = "Howdy";

Jul 23 '05 #2

"Shezan Baig" <sh************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Generic Usenet Account wrote:
I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};
My GCC compiler (g++ version 2.95.2) is giving the following

misleading
compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'
I suspect that initialization of static data members that are not of

an
integral type is not allowed in C++, but the compiler is giving a

wrong
error message. Kindly confirm/refute my observation.

Thanks,
Gus


I'm not sure if this will work, but try:

static const char* const yy = "Howdy";


That won't work, either. As the OP thought, you're not allowed to
initialize static data members in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).

Like you, I was thinking that the reason the compiler is saying that's
"non-const" is that it's a const ponter, but to non-const data. But making
it const data won't help here. It has to be an integral type to be
initialized "in class".

-Howard

Jul 23 '05 #3
Shezan Baig wrote:
Generic Usenet Account wrote:
I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};
My GCC compiler (g++ version 2.95.2) is giving the following


misleading
compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'
'yy' is definitely non-const. It would be 'const' if "const" were
sitting right in front of him. See the declaration below (which is
not going to work anyway).
I suspect that initialization of static data members that are not of


an
integral type is not allowed in C++, but the compiler is giving a


wrong
error message. Kindly confirm/refute my observation.
Seems like you're incorrect. However, the fact that you got confused
by the message may allow qualifying the message as 'confusing', but not
misleading.

Thanks,
Gus



I'm not sure if this will work, but try:

static const char* const yy = "Howdy";


This will NOT work. You are only allowed to initialise static const
members of _integral_ types.

V
Jul 23 '05 #4
Howard wrote:
[..] As the OP thought, you're not allowed to
initialize static data members
....static _const_ data members...

(Just to pick a nit)
in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).
[..]

V
Jul 23 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:g7*******************@newsread1.mlpsca01.us.t o.verio.net...
Howard wrote:
[..] As the OP thought, you're not allowed to initialize static data
members


...static _const_ data members...

(Just to pick a nit)
in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).
[..]

Quite correct. Thanks. (I hate those nits!)

-Howard
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more...
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
18
by: Nathan | last post by:
If you're wondering why I post so many questions, it's because I want to make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
11
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can...
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
6
by: Rune Allnor | last post by:
Hi folks. I naively tried to define some useful constants inside a namespace: namespace me{ #define PI = 3.14; }; It works when I refer to the constant from within that namespace, but...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.