initializing float in a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john smith

    #1

    initializing float in a class

    Hi,

    I am trying to initialize a float in a class.

    For example:

    class ABC{

    public:


    private:
    static const float = 3.3;

    };

    The compiler does not like that, and I don't know how to declare and define
    a float that has class scope only.

    Thanks in advance for your help.


  • Mike Wahler

    #2
    Re: initializing float in a class


    "john smith" <princetonharva rd@charter.net> wrote in message
    news:f0JCd.3916 $fX3.1840@fe03. lga...[color=blue]
    > Hi,
    >
    > I am trying to initialize a float in a class.
    >
    > For example:
    >
    > class ABC{
    >
    > public:
    >
    >
    > private:
    > static const float = 3.3;
    >
    > };
    >
    > The compiler does not like that, and I don't know how to declare and[/color]
    define[color=blue]
    > a float that has class scope only.[/color]

    Only static constant integer types can be initialized
    inside the class body. You also failed to give the
    'float' object a name.

    class ABC
    {
    public:

    private:
    static const float f;
    };

    const float ABC::f(3.3f);



    I recommend preferring type 'double' over 'float' unless
    you have a compelling reason to use the latter. 'double'
    gives you much better precision.

    -Mike


    Comment

    • Mike Hewson

      #3
      Re: initializing float in a class

      john smith wrote:[color=blue]
      > Hi,
      >
      > I am trying to initialize a float in a class.
      >
      > For example:
      >
      > class ABC{
      >
      > public:
      >
      >
      > private:
      > static const float = 3.3;[/color]

      'static const float' is a type but you have no identifier.

      Anyway I think only integrals are allowed for 'static const'.
      [color=blue]
      >
      > };
      >
      > The compiler does not like that, and I don't know how to declare and define
      > a float that has class scope only.
      >
      > Thanks in advance for your help.
      >
      >[/color]

      --

      Cheers
      --
      Hewson::Mike
      "This letter is longer than usual because I lack the time to make it
      shorter" - Blaise Pascal

      Comment

      • Ioannis Vranos

        #4
        Re: initializing float in a class

        john smith wrote:
        [color=blue]
        > Hi,
        >
        > I am trying to initialize a float in a class.
        >
        > For example:
        >
        > class ABC{
        >
        > public:
        >
        >
        > private:[/color]


        You forgot an identifier.

        static const float x = 3.3;[color=blue]
        >
        > };[/color]



        However are you sure about the private part?




        --
        Ioannis Vranos


        Comment

        • Ioannis Vranos

          #5
          Re: initializing float in a class

          Mike Hewson wrote:
          [color=blue]
          > Anyway I think only integrals are allowed for 'static const'.[/color]


          Yes you are right. Here is also what VC++ 2003 and 2005 Beta say about it:


          C:\c>cl /clr temp.cpp
          Microsoft (R) C/C++ Optimizing Compiler Version 13.10.3077 for .NET
          Framework
          Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

          temp.cpp
          temp.cpp(4) : error C2864: 'x' : only const static integral data members
          can be
          initialized inside a class or struct

          C:\c>


          C:\c>cl /clr temp.cpp
          Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40904
          for Microsoft (R) .NET Framework version 2.00.40607.16
          Copyright (C) Microsoft Corporation. All rights reserved.

          temp.cpp
          temp.cpp(4) : error C2864: 'ABC::x' : only static const integral data
          members ca
          n be initialized within a class

          C:\c>




          --
          Ioannis Vranos


          Comment

          • Ulrich Achleitner

            #6
            Re: initializing float in a class

            if you need a const static float attribute in your class,
            initialize it in the class's implementation file:

            const float ClassName::floa tAttributeName = 2.2f;

            On Tue, 4 Jan 2005 19:11:39 -0800, john smith
            <princetonharva rd@charter.net> wrote:
            [color=blue]
            > Hi,
            >
            > I am trying to initialize a float in a class.
            >
            > For example:
            >
            > class ABC{
            >
            > public:
            >
            >
            > private:
            > static const float = 3.3;
            >
            > };
            >
            > The compiler does not like that, and I don't know how to declare and
            > define
            > a float that has class scope only.
            >
            > Thanks in advance for your help.
            >
            >[/color]



            --
            have a nice day
            ulrich

            Comment

            Working...