C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 30th, 2005, 04:46 AM
Spifficus Spifficus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 Spifficus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 32 sec
Reputation Power: 0
Question Array of char pointers - declaration problem

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

Reply With Quote
  #2  
Old May 30th, 2005, 09:01 AM
ShadowCoder ShadowCoder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Ottawa
Posts: 20 ShadowCoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 12 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old May 30th, 2005, 08:35 PM
Spifficus Spifficus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 Spifficus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 32 sec
Reputation Power: 0
Thanks

Very helpful explanation. Thanks for the redirection to CStringArray. Much obliged, ShadowCoder.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Array of char pointers - declaration problem


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap