g++ strange problem

Discussion in 'C++' started by Chameleon, Apr 3, 2011.

  1. Chameleon

    Chameleon Guest

    With g++ I have this error when I compile:
    ----------crap.cpp--------------------------------------------------------
    struct A {
    static const double CRAP = 1;
    A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
    A(int) { double a = - (CRAP / 2); }
    };

    int main() {
    A a;
    A b(0);
    return 0;
    }
    --------------------------------------------------------------------------
    Why?
    It is compiler specific?
    Chameleon, Apr 3, 2011
    #1
    1. Advertising

  2. Chameleon

    Balog Pal Guest

    "Chameleon" <>
    > With g++ I have this error when I compile:
    > ----------crap.cpp--------------------------------------------------------
    > struct A {
    > static const double CRAP = 1;
    > A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
    > A(int) { double a = - (CRAP / 2); }
    > };
    >
    > int main() {
    > A a;
    > A b(0);
    > return 0;
    > }
    > --------------------------------------------------------------------------
    > Why?
    > It is compiler specific?


    in-class static const with init is limited to integral types, so you should
    get an error right at second line. guess you just skipped over it. If not,
    your code is still ill-formed, and you got a diagnostic, even if not the
    most helpful one.
    Balog Pal, Apr 3, 2011
    #2
    1. Advertising

  3. Chameleon wrote, On 3.4.2011 20:05:
    > With g++ I have this error when I compile:
    > ----------crap.cpp--------------------------------------------------------
    > struct A {
    > static const double CRAP = 1;

    Only static const members of integral (int etc.) types can be initialized
    inline. Double type is not integral type. This should not even compile.

    > A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
    > A(int) { double a = - (CRAP / 2); }
    > };


    You are missing a definition of the constant member:

    > const double A::CRAP;


    >
    > int main() {
    > A a;
    > A b(0);
    > return 0;
    > }
    > --------------------------------------------------------------------------
    > Why?

    The standard says so. Static member variables (constant or otherwise) need
    out-of-line definition.

    > It is compiler specific?

    No, though some compilers might not issue any error because they optimize any
    references to the constant member away.

    --
    VH
    Vaclav Haisman, Apr 3, 2011
    #3
  4. Chameleon

    Chameleon Guest

    > Chameleon wrote, On 3.4.2011 20:05:
    >> With g++ I have this error when I compile:
    >> ----------crap.cpp--------------------------------------------------------
    >> struct A {
    >> static const double CRAP = 1;

    > Only static const members of integral (int etc.) types can be initialized
    > inline. Double type is not integral type. This should not even compile.


    Whow! double is not integral? Why? (long double actually)


    >> A() { double a = - CRAP / 2; } // Undefined reference to A::CRAP
    >> A(int) { double a = - (CRAP / 2); }
    >> };

    >
    > You are missing a definition of the constant member:
    >
    >> const double A::CRAP;

    >
    >>
    >> int main() {
    >> A a;
    >> A b(0);
    >> return 0;
    >> }
    >> --------------------------------------------------------------------------
    >> Why?

    > The standard says so. Static member variables (constant or otherwise) need
    > out-of-line definition.
    >
    >> It is compiler specific?

    > No, though some compilers might not issue any error because they optimize any
    > references to the constant member away.


    Yes, g++, has not a problem. Program works just fine. But if it is out
    of standard I will change it.

    I think that I read on "Thinking in C++" that this code is correct:
    -----
    class A {
    static const int AAAA = 1;
    };
    -----
    so, the static const double AAAA = 1.0; is very different?
    Chameleon, Apr 4, 2011
    #4
  5. Chameleon

    Jonathan Lee Guest

    On Apr 3, 7:14 pm, Chameleon <> wrote:
    > > Chameleon wrote, On 3.4.2011 20:05:
    > >> With g++ I have this error when I compile:
    > >> ----------crap.cpp--------------------------------------------------------
    > >> struct A {
    > >>      static const double CRAP = 1;

    > > Only static const members of integral (int etc.) types can be initialized
    > > inline. Double type is not integral type. This should not even compile.

    >
    > Whow! double is not integral? Why? (long double actually)


    Basically, by definition. Integral types are things that are
    (roughly speaking) like integers. Double can have fractions,
    so that pretty much means they aren't integral types.

    As far as C++ is concerned, though, some types are explicitly
    stated to be "integral types". Double is not one of those
    types.

    --Jonathan
    Jonathan Lee, Apr 4, 2011
    #5
  6. Chameleon

    crea Guest

    "Chameleon" <> wrote in message
    news:inb2hb$qd9$...
    >
    > I think that I read on "Thinking in C++" that this code is correct:
    > -----
    > class A {
    > static const int AAAA = 1;
    > };
    > -----
    > so, the static const double AAAA = 1.0; is very different?
    >


    It seems be allowed only with integrals:
    http://en.wikipedia.org/wiki/One_Definition_Rule
    "In pre-standard C++, all static data members required a definition outside
    of their class. However, during the C++ standardization process it was
    decided to lift this requirement for static const integral members." Double
    is not an integral...
    crea, Apr 4, 2011
    #6
  7. Chameleon

    Drew Lawson Guest

    In article <>
    Jonathan Lee <> writes:
    >On Apr 3, 7:14 pm, Chameleon <> wrote:
    >> > Chameleon wrote, On 3.4.2011 20:05:
    >> >> With g++ I have this error when I compile:
    >> >> ----------crap.cpp--------------------------------------------------------
    >> >> struct A {
    >> >>      static const double CRAP = 1;
    >> > Only static const members of integral (int etc.) types can be initialized
    >> > inline. Double type is not integral type. This should not even compile.

    >>
    >> Whow! double is not integral? Why? (long double actually)

    >
    >Basically, by definition. Integral types are things that are
    >(roughly speaking) like integers. Double can have fractions,
    >so that pretty much means they aren't integral types.


    The clarify what I think may have been the OP's confusion, "integral"
    has multiple meanings. Outside of math and computer language
    definitions, the more common usage is relating to a part of the
    whole. So "integral types" and "built-in types" might look like
    the same thing.


    --
    Drew Lawson | What is an "Oprah"?
    | -- Teal'c
    |
    Drew Lawson, Apr 4, 2011
    #7
  8. Chameleon

    Geoff Guest

    On Mon, 04 Apr 2011 03:14:34 +0300, Chameleon <>
    wrote:

    >Whow! double is not integral? Why? (long double actually)


    Double is a floating point type. Integral types are integers.
    Geoff, Apr 4, 2011
    #8
  9. On Apr 3, 11:05 am, Chameleon <> wrote:
    > With g++ I have this error when I compile:
    > ----------crap.cpp--------------------------------------------------------
    > struct A {
    >      static const double CRAP = 1;
    >      A() { double a = - CRAP / 2; }    // Undefined referenceto A::CRAP
    >      A(int) { double a = - (CRAP / 2); }
    >
    > };
    >
    > int main() {
    >      A a;
    >      A b(0);
    >      return 0;}
    >
    > --------------------------------------------------------------------------
    > Why?
    > It is compiler specific?


    Is not the "undefinded reference to A::CRAP" a result of incorrect
    initialization of the "static const" variable?

    The static const variable should be only declared within the struct
    scope. I believe the static (const or not) should be defined outside
    the structure scope as:

    static const double A::CRAP = 1; // or 1.0 to be more explicit

    Todd S.
    northerntechie, Apr 5, 2011
    #9
    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. Kload

    Strange Timing Problem

    Kload, Oct 23, 2003, in forum: VHDL
    Replies:
    2
    Views:
    573
    Kload
    Oct 23, 2003
  2. Akshaye
    Replies:
    1
    Views:
    717
    Hemant Gupta
    Feb 9, 2004
  3. Pedro Miguel Carvalho
    Replies:
    2
    Views:
    1,111
    Pedro Miguel Carvalho
    Oct 25, 2004
  4. Harvey Twyman
    Replies:
    8
    Views:
    549
    August Derleth
    Oct 25, 2003
  5. championsleeper
    Replies:
    0
    Views:
    565
    championsleeper
    Jan 26, 2005
Loading...

Share This Page