473,466 Members | 4,869 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

header file array declaration

112 New Member
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
Oct 9 '07 #1
8 16977
RedSon
5,000 Recognized Expert Expert
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.
Oct 9 '07 #2
drsmooth
112 New Member
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
Oct 9 '07 #3
RedSon
5,000 Recognized Expert Expert
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.
Oct 9 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Oct 9 '07 #5
RedSon
5,000 Recognized Expert Expert
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.
Oct 10 '07 #6
drsmooth
112 New Member
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?
Oct 10 '07 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Expand|Select|Wrap|Line Numbers
  1. class coin
  2. {
  3.     bool face;
  4.     public:
  5.       bool GetFace()
  6. };
  7.  
Then you can:
Expand|Select|Wrap|Line Numbers
  1. class Coin
  2. {
  3.     bool face;
  4.     public:
  5.       bool GetFace()
  6. };
  7. bool Coin::GetFace()
  8. {
  9.     return face;
  10. }
  11.  
  12. int main()
  13. {
  14.      Coin c;
  15.      bool face = c.GetFace();
  16. }
  17.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include <coin.h>
  2.  
  3. bool Coin::GetFace()
  4. {
  5.     return face;
  6. }
  7.  
  8. int main()
  9. {
  10.      Coin c;
  11.      bool face = c.GetFace();
  12. }
  13.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include <coin.h>
  2.  
  3. bool Coin::GetFace()
  4. {
  5.     return face;
  6. }
  7.  
leaving you just this:
Expand|Select|Wrap|Line Numbers
  1. #include <coin.h>
  2.  
  3. int main()
  4. {
  5.      Coin c;
  6.      bool face = c.GetFace();
  7. }
  8.  
in the file with main().
Oct 10 '07 #8
drsmooth
112 New Member
now i am trying to declare a char[][] like this:

Granery::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

while in the header i have:


Expand|Select|Wrap|Line Numbers
  1. #ifndef Granery_h
  2. #define Granery_h
  3. class Granery
  4. {
  5.   public:
  6.    Granery();
  7.    static char foodNames[5][100];
  8. };
  9. #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::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

i have also tried:
Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

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

thanks,
ken
Oct 15 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
0
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in...
4
by: Newsgroup - Ann | last post by:
I have a library function like GetOptions( ..., struct net_arch_t netArch, ....) and put the declaration into a regular header file like getopt.h. But this function declaration also needs the...
5
by: G?nter Omer | last post by:
Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL <list> but it refuses to work. The following...
6
by: candy | last post by:
hi all, I just want to know that whether the C header files( like stdio.h,etc which the compiler provides) just contains the function declarations or they also contain some additionalinformation...
13
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
2
by: free2klim | last post by:
Hi, I am relatively new to programming C++ and I am writing a small program in which I am using a 3D array of type int. The 3D array is a member of class GameBoard, which I have defined in the...
2
by: beet | last post by:
Hi all, I tried to declare a c++ struct like following in a header file; I want to include this header file in other files to create and access this struct. ------ 1 #ifndef _SEARCHDATA_H_...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.