| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Greetings all. Am discovering quickly that methods of declaration in Java are not the same in C++.
My intent is to declare a static array of char pointers; this array would hold various output messages used in a parsing function. The idea is to be able to call the message by array subscript rather than list the entire text each time I need it. I am attempting to declare it in the header file as such: Code:
static const char* messages[NUM_MESSAGES] = {"The entry includes more than one minus sign.",
"The entry has a minus sign following a character.",
"The entry includes more than one decimal.",
"The entry includes at least two characters separated by a space.",
"The entry includes a non-digit character.",
"The entry consists of only a decimal point.",
"The entry consists of only a minus sign.",
"No entry was made."};
...and I am learning that the compiler does not like this (Microsoft Visual C++). The following two compile errors result: Code:
RectanglesIODriver.h(61): error C2059: syntax error : '{'
RectanglesIODriver.h(61): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
My question is how to most efficiently declare/create the array. I can get it to work if it is declared and created inside a function, but if I am correct this provides only function scope, and creates overhead each time the function is called. I would like to give the array file scope, so that I can call on it from any function. Appreciate the help of any samaritans that may be willing to explain it to me. Charles |
|
#2
|
|||
|
|||
|
The problem is that you can't set the value of your array inside the header file, specifically the class declaration, you have to do it in the .cpp file. In Java you've only got the .java file, which in terms of C++ is sort of like the .h and .cpp files combined in one. The best place to set it would probably be in the constructor. Although you could do something like:
Code:
char (*messages)[_MAX_PATH] = new char[NUM_MESSAGES][_MAX_PATH]; strcpy(&messages[0][0], "string1."); strcpy(&messages[1][0], "string2."); delete[] messages; I would suggest, because you're using VC++, to look into the CStringArray class. Then you can simply do something like: Code:
CStringArray array;
array.Add("string1");
array.Add("string2");
and this way you don't have to worry about allocating and freeing memory yourself. It also has the benefits of sporting a ton of methods that make life easy. The CStringArray would still have to be declared in the header and the values for it set somewhere in the .cpp. |
|
#3
|
|||
|
|||
|
Thanks
Very helpful explanation. Thanks for the redirection to CStringArray. Much obliged, ShadowCoder.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Array of char pointers - declaration problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|