initializing a constant in the .h file?

Discussion in 'C++' started by anonymous@coolgroups.com, Jul 31, 2004.

  1. Guest

    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
    , Jul 31, 2004
    #1
    1. Advertising

  2. On Sat, 31 Jul 2004 11:29:58 GMT, <> 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
    John Harrison, Jul 31, 2004
    #2
    1. Advertising

  3. On Sat, 31 Jul 2004 12:44:20 +0100, John Harrison
    <> wrote:

    > On Sat, 31 Jul 2004 11:29:58 GMT, <> 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
    John Harrison, Jul 31, 2004
    #3
  4. On 7/31/2004 1:29 PM, 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
    =?ISO-8859-2?Q?Mateusz_=A3oskot?=, Jul 31, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,086
  2. Martin Magnusson
    Replies:
    2
    Views:
    503
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    559
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    338
    Keith Thompson
    Dec 14, 2006
  5. Pavan
    Replies:
    9
    Views:
    2,580
    James Kanze
    Mar 4, 2008
Loading...

Share This Page