Defining a const as a data member of a class

Discussion in 'Programmer's Corner' started by mentaaal, Mar 11, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi all,
    quick question: I am fiddling around with classes and am trying to define a private data member: const float PIE but the compiler i am using (Microsoft Visual C++ 2005 version 8 says:

    1>c:\users\greg\documents\visual studio 2005\projects\area calculations\area calculations\geometry definition.cpp(7) : error C2758: 'geometry::pIE' : must be initialized in constructor base/member initializer list

    When i try and do this( I typed the following:

    geometry::geometry()
    {
    const geometry::pIE = 3.14159;
    }

    in the constructor says:

    c:\users\greg\documents\visual studio 2005\projects\area calculations\area calculations\geometry.h(17) : see declaration of 'geometry::pIE'
    1>c:\users\greg\documents\visual studio 2005\projects\area calculations\area calculations\geometry definition.cpp(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\greg\documents\visual studio 2005\projects\area calculations\area calculations\geometry definition.cpp(8) : error C2761: 'const float geometry::pIE' : member function redeclaration not allowed
    1>c:\users\greg\documents\visual studio 2005\projects\area calculations\area calculations\geometry definition.cpp(8) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    1>Build log was saved at "file://c:\Users\Greg\Documents\Visual Studio 2005\Projects\area calculations\area calculations\Debug\BuildLog.htm"
    1>area calculations - 4 error(s), 0 warning(s)


    Can anyone explain to me what I should do to define a private data member as a const?

    Thanks!

    P.S. i realised that i didnt stick in "float" iin the definition but i did that after writing this and the same result occurrs...
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Code ( (Unknown Language)):
    1.  
    2. geometry::geometry() :
    3.   PIE(3.14159)
    4. {
    5. }
    6.  
     
Loading...