Thread: class constants

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    class constants

    Hi i'm trying to create a constant variable from within my class, but i just get the error
    [C++ Error] Test.h(40): E2233 Cannot initialize a class member here
    Do you have to declare 2d arrays differently from within the class?

    Code:
      static const char Test[3][20] = {{"Test1"}, {"Test2"}, {"Test3"}};
    Thanks.
    Be a leader and not a follower.

  2. #2
    stovellp
    Guest
    You cant initialise a variable inside a class.
    I'm not sure, but I think you'd have to do it in the constructor.


    Code:
    class myClass
       {
       public:
          myClass();
          const int Height;
       };
    
    myClass::myClass()
       {
       Height = 67;
       }
    But if its a static variable, you define it outside the class, like:

    Code:
    const char myClass::Test[3][20] = {{"Test1"}, {"Test2"}, {"Test3"}};

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by stovellp
    You cant initialise a variable inside a class.
    Yes, static const variables can be.
    Code:
    //Valid C++
    class A
    {
      static const int Test  = 1;
    };
    I'm not sure why arrays can't be initialized the same way, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Only const integers can be done like that sang-drax, nothing else.
    Last edited by Polymorphic OOP; 01-25-2003 at 06:37 PM.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, but why are only integral basic data types allowed?
    I see no reason.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    I wonder that same thing. Not even floating point types can be initialized in a class definition. There's probably some logical reason why they didn't, but I honestly can't think of one.

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Hi all, thanks for your replies. I've just had to make the 2d array global, thats the only way i've managed to implement the class.
    Be a leader and not a follower.

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I've just had to make the 2d array global, thats the only way i've managed to implement the class.
    Initialize the array just like you do member functions, the declaration is in the class and the definition is outside of the class with named scoping.
    Code:
    class test {
      static const char Test[3][20];
      ...
    };
    
    const char test::Test[3][20] = {"Test1", "Test2", "Test3"};
    *Cela*

  9. #9
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    erm.... interesting. I'll have a go at that, thanks cela.
    Be a leader and not a follower.

  10. #10
    stovellp
    Guest
    Isn't that what I said in the first place?

  11. #11
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Isn't that what I said in the first place?
    Yep, but important concepts are best repeated. :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM