how to initialize declared 2d character array

This is a discussion on how to initialize declared 2d character array within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a header file with a declaration like this: Code: class MyClass { char ch_map[][4]; } then in ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    how to initialize declared 2d character array

    Hi,

    I have a header file with a declaration like this:
    Code:
    class MyClass 
    {   
       char ch_map[][4];
    }
    then in the cpp file I want to have something like this:
    Code:
    #include "header.h"
    MyClass::MyClass ()
    {
        char ch_map = {"abcd", "efgh", "ijkl"}; 
    }
    but obviously I can't redeclare ch_map in the cpp file. What's the best way to initialize the ch_map array within the constructor?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And for that matter you can't declare ch_map with an empty array size either. Do you want a static variable? Or are "abcd", "efgh", and "ijkl" (which by the way can't be stored in ch_map the way it is defined, since they would require five characters) going to change, which would suggest initializer lists? And why aren't we using std::string anyway?

    Edit: even better, a std::vector of std::strings, since you wouldn't need to give a size ahead of time! Just push!
    Last edited by tabstop; 03-13-2008 at 09:13 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Hi,
    Thanks for your reply.
    Yes I should be more clear. ch_map will be constant and the size will not change. Also there should be only 3 characters in the initialization strings (I actually was initializing them one by one {'a', 'b', 'c', 'd'} etc.. ). How do I make it a constant static array? I tried in the header file:
    Code:
    static char ch_map[3][4] = {{'a', 'b', 'c', 'd'}, {'e', 'f', etc.... }};
    but that also didn't work.

    Thanks for the help.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A static member variable means there's only one, for all the objects to use. So if it's constant, and it doesn't matter that all your MyClasses will have the same one, then that's you want. Quoting:
    Quote Originally Posted by cplusplus.com
    In order to initialize a static data-member we must include a formal definition outside the class, in the global scope.
    I haven't tried to do a static array, so I don't know if it's possible. Otherwise, you'll have to just declare the variable in the header and assign values in the constructor (for loops will be your friend).

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think in the header for your class:
    Code:
    //Declare ch_map
    static char ch_map[3][4];
    Then somewhere in the cpp file:
    Code:
    //Define ch_map
    static char MyClass::ch_map[3][4] = {{'a', 'b', 'c', 'd'}, {'e', 'f', etc.... }};

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Yeah you're right. I put the array outside of the class definition and made it static.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I put the array outside of the class definition and made it static.
    But it's still declared within the class, right?
    Code:
    class MyClass
    {
    //Declare ch_map
    static char ch_map[3][4];
    };

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I just tried it, you need to omit static when you define it.
    Code:
    //Header file
    class MyClass
    {
        static char ch_map[3][4];
    };
    Code:
    //cpp file
    char MyClass::ch_map[3][4] = {{'a', 'b', 'c', 'd'}, {'e', 'f', etc.... }};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 1
    Last Post: 10-21-2007, 08:44 AM
  3. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21