LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-02-2005, 10:31 AM   #1
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Rep: Reputation: 44
Initializing Array of Strings


I have a header file and definition file for a C++ class. I want to initialize an array of strings with the values below, but I keep getting this:

date.cpp:13: error: ISO C++ forbids declaration of `monthNames' with no type
date.cpp:13: error: initializer for scalar variable requires one element
date.cpp:14: error: parse error before `}' token


what do I need to do to initialize this array? here are my declaration and attempted initialization:

from date.h:
string monthNames[12];

from date.cpp:
monthNames = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Nov.", "Dec."};

thanks,
jrtayloriv
 
Old 02-02-2005, 10:34 AM   #2
freeka
Member
 
Registered: Feb 2005
Posts: 57

Rep: Reputation: 15
im only programming c but there it looks like this

char *monthNames[] = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Nov.", "Dec."};
 
Old 02-02-2005, 10:42 AM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
You need to initialize it when you declare it:

Code:
#include <string>
 using std::string;

string monthNames[13] = { "", "Jan.", "Feb." ... "Dec." };
Also, if you have 13 elements with the first empty, monthNames[1] is "Jan." Easier to work with!

Hope that helps.
 
Old 02-02-2005, 10:50 AM   #4
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Original Poster
Rep: Reputation: 44
I cannot initialize it when I declare it, because it is declared in a class header file.

I posted all of the source on my site (too long to post here) so you can get a better idea what I am trying to do. This program is full of errors, and I am just trying to fix this one first. If you see anything else I could fix, please let me know.

http://jtlin.dyndns.org/help.html

thanks,
jrtayloriv
 
Old 02-02-2005, 10:57 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Declare it as extern in the header, and then declare/initialize it in the .cpp

Code:
// The header
#include <string>

extern std::string monthNames[13];

// the .cpp
std::string monthNames[13] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 
Old 02-02-2005, 11:04 AM   #6
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Original Poster
Rep: Reputation: 44
did what you said, now I'm getting this:

In file included from date.cpp:5:
date.h:32: error: storage class specified for field `monthNames'
In file included from date.cpp:5:
date.h:36:7: warning: no newline at end of file
date.cpp:8: error: ISO C++ forbids defining types within return type
date.cpp:8: error: return type specification for constructor invalid
date.cpp: In constructor `date::date()':
date.cpp:12: error: parse error before `{' token
date.cpp: At global scope:
date.cpp:14: error: parse error before `}' token
date.cpp:134:2: warning: no newline at end of file

Maybe the array wasn't actually the problem, check http://jtlin.dyndns.org/help.html to see the whole mess

thanks again,
jrtayloriv
 
Old 02-02-2005, 11:26 AM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Hmmm... is this a member variable in a class? I assumed it was a global. I'll take a look at the full code if I get the chance. You could probably make it static and const as well, since the data probably won't be changing between instances of the class, unless you plan on doing some globalization thing.
 
Old 02-02-2005, 11:54 AM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Ok, I took a quick look at the date class and I got that portion to compile at least. I didn't look it over for logic errors, or try compiling the datedriver class, but this should get you started.

In date.h change the declaration of these two member variables to be like so:

Code:
     static const string monthNames[12];
     static const unsigned short monthDays[12];
Then in the date.cpp file, define them like so. Note that this is NOT in the constructor, but outside of all functions just as though they were globals.

Code:
const string date::monthNames[12] =
{
    "Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
    "Jul.", "Aug.", "Sep.", "Nov.", "Dec."
};

const unsigned short date::monthDays[12] =
{
    31, 28, 31, 30, 30, 31, 31, 31, 30, 31, 30, 30
};
Note that I used the same data you used for monthDays, though that data is incorrect. Not sure what calendar you are using, but mine goes 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31. Since you are using the Jan, Feb, Mar, etc. I assume you are just using a standard calendar and not some funky one.

Obviously, you should also remove the initialization of these two variables from the constructor as well.
 
Old 02-02-2005, 01:39 PM   #9
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Original Poster
Rep: Reputation: 44
Thanks, that is what I needed to do. Just for future reference, why did I need to declare them global and const? What does this do in terms of how the data is stored when the program runs? And thanks for the heads-up on the incorrect calendar dates - getting this to work wouldn't have done me much good with that screwed up.

Thanks a bunch,
jrtayloriv
 
Old 02-02-2005, 02:25 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Well, they aren't technically global, they are static. That equates to almost the same thing, though. You could have kept them as they were, but that seemed rather wasteful because every single instance of the date class would then have it's own instance of those arrays. Making them static essentially makes it so that every instance of the date class uses the same memory for those variables. Since it is still private, the only thing that can access the variable is still just your class, though. If it were public, other things could access them via date::monthNames[0], etc.

I added the const to them because that data should not ever be changing, so making it const just makes the compiler enforce that nothing tries to change it. It's not necessary, just good practice.

If you had wanted to keep them like you had them, as members of each and every instance of date, I'm afraid the only option you would have had would be to do either something like so:

Code:
date::date()
{
   monthNames[0] = "Jan";
   monthNames[1] = "Feb";
   // etc.
    
}
Or to do something like so:

Code:
date::date()
{
    std::string tempNames[12] = {"Jan", "Feb", ...};

    for (int c=0;c<12;c++)
    {
        monthNames[c] = tempNames[c];
    }
}

Last edited by deiussum; 02-02-2005 at 02:29 PM.
 
Old 02-03-2005, 01:22 PM   #11
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Original Poster
Rep: Reputation: 44
Yeah, I didn't think about the fact that I didn't actually need one for every instance. It makes sense now, why I needed to put them outside. Thanks again.

jrtayloriv
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
strings in c djgerbavore Programming 8 01-11-2005 05:27 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 11:58 AM
[C programming] sorting strings in an array miguetoo Programming 1 05-26-2003 02:48 AM


All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration