Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initializing a constant in the .h file?

Reply
Thread Tools

initializing a constant in the .h file?

 
 
anonymous@coolgroups.com
Guest
Posts: n/a
 
      07-31-2004
hello,
i am attempting to initialize a constant in my .h file. it would be terrific if i
could also assign the constant, but i continue to get compile problems. this
is what i'm trying to do:

(in myfile.h)
const std::string MY_CONSTANT = "permanent value";

but my compiler (gcc version 3.2.1) tells me:

ISO C++ forbids initialization of member

any feedback is appreciated
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-31-2004
On Sat, 31 Jul 2004 11:29:58 GMT, <(E-Mail Removed)> wrote:

> hello,
> i am attempting to initialize a constant in my .h file. it would be
> terrific if i
> could also assign the constant, but i continue to get compile problems.
> this
> is what i'm trying to do:
>
> (in myfile.h)
> const std::string MY_CONSTANT = "permanent value";
>
> but my compiler (gcc version 3.2.1) tells me:
>
> ISO C++ forbids initialization of member
>
> any feedback is appreciated


Well C++ does indeed forbid in-class initialisation of constants, except
for integral constants.

So you have to initialise your constant outside the class, in a source
file, instead. Is there any reason why this is a problem?

john
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-31-2004
On Sat, 31 Jul 2004 12:44:20 +0100, John Harrison
<(E-Mail Removed)> wrote:

> On Sat, 31 Jul 2004 11:29:58 GMT, <(E-Mail Removed)> wrote:
>
>> hello,
>> i am attempting to initialize a constant in my .h file. it would be
>> terrific if i
>> could also assign the constant, but i continue to get compile
>> problems. this
>> is what i'm trying to do:
>>
>> (in myfile.h)
>> const std::string MY_CONSTANT = "permanent value";
>>
>> but my compiler (gcc version 3.2.1) tells me:
>>
>> ISO C++ forbids initialization of member
>>
>> any feedback is appreciated

>
> Well C++ does indeed forbid in-class initialisation of constants, except
> for integral constants.
>
> So you have to initialise your constant outside the class, in a source
> file, instead. Is there any reason why this is a problem?
>
> john


I missed that you haven't declared your constant as static, is there any
reason for this?

Normally you would do something like this

// in header file
class MyClass
{
static std::string MY_CONSTANT;
};

// in source file
std::string MyClass::MY_CONSTANT = "permanent value";

john
 
Reply With Quote
 
=?ISO-8859-2?Q?Mateusz_=A3oskot?=
Guest
Posts: n/a
 
      07-31-2004
On 7/31/2004 1:29 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> (in myfile.h)
> const std::string MY_CONSTANT = "permanent value";


I believe it will help:

http://tinyurl.com/4oanj

Greets

--

Mateusz Łoskot
mateusz at loskot dot net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
declaring constant array without initializing all the elements Pavan C++ 9 03-04-2008 10:42 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments