header file array declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    #1

    header file array declaration

    this is kind of two questions:

    1. do i have to define class variables in a header file?

    2. if i do how do i use an initializer list for an array of strings in the header?(the array can be static)

    thanks
    ken
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by drsmooth
    this is kind of two questions:

    1. do i have to define class variables in a header file?

    2. if i do how do i use an initializer list for an array of strings in the header?(the array can be static)

    thanks
    ken
    For non global variables they need to be inside your class declaration.
    To initialize an array you can do int array[5] = {0}; I think that works for strings to, most of my string work is with pointers so I cant really remember.

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      if i use an initializer list in the header file i get a compiler error saying i can't "initialize a class member here"

      im a two year java student trying to rollover to c++ so this header stuff is kinda new to me...

      thanks
      ken

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by drsmooth
        if i use an initializer list in the header file i get a compiler error saying i can't "initialize a class member here"

        im a two year java student trying to rollover to c++ so this header stuff is kinda new to me...

        thanks
        ken
        Ahh you are trying to do it in a header. That won't work, try initializing it in the constructor.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          drsmooth:

          You do not define variables in a header file.

          Header files are to contain nothing that allocates memory or generates code. If you do, then you get duplicate definitions each time you include the header.

          Header files are for declarations:
          1) I declare this class, (the class declaration)
          2) I declare this struct, (the struct declaration)
          3) I declare this enum, (the enum enumeration)
          4) I declare this function, (the function prototype)
          5) I declare this variable is defined ouside this file (the extern)
          etc.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by weaknessforcats
            drsmooth:

            You do not define variables in a header file.

            Header files are to contain nothing that allocates memory or generates code. If you do, then you get duplicate definitions each time you include the header.

            Header files are for declarations:
            1) I declare this class, (the class declaration)
            2) I declare this struct, (the struct declaration)
            3) I declare this enum, (the enum enumeration)
            4) I declare this function, (the function prototype)
            5) I declare this variable is defined ouside this file (the extern)
            etc.
            Don't forget about inline methods, but that is like a whole different ball of wax.

            Comment

            • drsmooth
              New Member
              • Oct 2007
              • 112

              #7
              ok but if i dont declare them in the header and i just declare a class variable is this a correct way to say it:

              (lets say a coin class and this is in the coin.cpp)

              int face = 0;

              Coin::Coin(){}

              would i be able to access like

              coin c();
              int x = c.face ;

              or something along those lines?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by drssmooth
                ok but if i dont declare them in the header and i just declare a class variable is this a correct way to say it:

                (lets say a coin class and this is in the coin.cpp)

                int face = 0;

                Coin::Coin(){}

                would i be able to access like

                coin c();
                int x = c.face ;

                or something along those lines?
                Not quite. To get the face of a coin you would need a variable for the face, say 1 fior heads an 0 for tails
                [code=c]
                class coin
                {
                bool face;
                public:
                bool GetFace()
                };
                [/code]

                Then you can:
                [code=cpp]
                class Coin
                {
                bool face;
                public:
                bool GetFace()
                };
                bool Coin::GetFace()
                {
                return face;
                }

                int main()
                {
                Coin c;
                bool face = c.GetFace();
                }
                [/code]

                and this has all of your code in one file. Not good.

                Better to put the class declaration in a header and inclue it otherwise you will have to paste a copy of the class declaration in all of your source files:
                [code=cpp]
                #include <coin.h>

                bool Coin::GetFace()
                {
                return face;
                }

                int main()
                {
                Coin c;
                bool face = c.GetFace();
                }
                [/code]

                Finally, if you need your Coin code in another program you can't do it without dragging the main() from this program along wioth the code. Better to put the Coin code in its own file:
                [code=cpp]
                #include <coin.h>

                bool Coin::GetFace()
                {
                return face;
                }
                [/code]

                leaving you just this:
                [code=c]
                #include <coin.h>

                int main()
                {
                Coin c;
                bool face = c.GetFace();
                }
                [/code]
                in the file with main().

                Comment

                • drsmooth
                  New Member
                  • Oct 2007
                  • 112

                  #9
                  now i am trying to declare a char[][] like this:

                  Granery::foodNa mes[5][100] = {"Meat","Fruit" ,"Wheat","Veget ables","Dairy"} ;

                  while in the header i have:


                  Code:
                  #ifndef Granery_h
                  #define Granery_h
                  class Granery
                  {
                    public:
                     Granery();
                     static char foodNames[5][100];
                  };
                  #endif
                  when i compile this i get an error :

                  [C++ Error] Granery.cpp(5): E2188 Expression syntax

                  that error is on the line in the .cpp code
                  Granery::foodNa mes[5][100] = {"Meat","Fruit" ,"Wheat","Veget ables","Dairy"} ;

                  i have also tried:
                  Granery::foodNa mes = {"Meat","Fruit" ,"Wheat","Veget ables","Dairy"} ;

                  any help with how i shud be initializing the char[][]?

                  thanks,
                  ken

                  Comment

                  Working...