Solved

can I initialize member like this?

Posted on 1997-11-12
5
286 Views
Last Modified: 2010-04-10
here is what I did:

class Date {
  private:
    int day;     // Stores the day, 1-31.
    int month; // Stores the month, 1 -12.
    int year;  // Stores the year.  Any by size of int.

    // List of months for converting to a value.
    char months[] = 3D { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                        "Sep", "Oct", "Nov", "Dec"};
  public:
    Date();
    Date(char *d);
    //{  // Constructor with the string for part 2.
    //  setDate(d);
    //}
    void setDate(const char *d);
    char *getDate() const;class Date {
  private:
    int day;     // Stores the day, 1-31.
    int month; // Stores the month, 1 -12.
    int year;  // Stores the year.  Any by size of int.

    // List of months for converting to a value.
    char months[] = 3D { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                        "Sep", "Oct", "Nov", "Dec"};
  public:
    Date(char *d);
    {  // Constructor with the string for part 2.
      setDate(d);
    }
    void setDate(const char *d);
    char *getDate() const;
...
};

I got an error msg: Can't initialize a member here.
on the line of char months[] = 3D {...};

why?

Thank you
0
Comment
Question by:calprcr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
Hardaway earned 0 total points
ID: 1173036
What do you mean by 3D??

I think the correct way is like the following :
char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

"Jan" is a string rather than a character.
4 instead of 3 because of a string separator.
0
 

Author Comment

by:calprcr
ID: 1173037
I got rid of 3D and do what you gave me. I still got the error msg.


0
 
LVL 22

Expert Comment

by:nietod
ID: 1173038
Hardaway's answer is syntaclly right to initialize an array.  But you can't initialize an array that is a class/structure member.  (or you can, but you must do it in the constructor.)

0
 
LVL 22

Expert Comment

by:nietod
ID: 1173039
Actually, I think the "real" problem is that you don't want the array to be a member, or if you do, you want it to be a static member.  The way it is not, as a non-static member, each data object will have its own (private, seperate) copy of the months array.  That is probably a waste.  You can either declare it global or static.  (Static will produce one copy that is shared among all the date objects).
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173040
Also multi-dimentional arrays in C++ are kinda weak.  I avoid them if I can.  For this kind of table, an array of character pointers usually works best.  Like

const char *Months = {"Jan","Feb",Mar"};

one advantage of this is that the strings can be different lengths, like

const char *Months = {"January","February",March"};

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
Quicksort a dynamic deque 33 79
TCP/IP Socket - connection close results in data lost 14 153
Socket Programming (Unix) 8 153
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question