-1

I have something that looks like this

class RestaurantCheck
{
  private:
  static const int MENU_LENGTH = 10;
  static const string menu[MENU_LENGTH] = {"Gumbo", "Shrimp", etc...}

Right off the bat, I have a problem. I know I can't initialize the data in the array as it is now, so I tried this ...

class RestaurantCheck
{
  private:
  static const int MENU_LENGTH = 10;
  static const string menu[MENU_LENGTH]; 
  void displayMenu();

  public:
  void showMenu()
  {
    RestaurantCheck thisMenu;
    thisMenu.displayMenu();
}

void RestaurantCheck::displaymenu()
{
 menu[0] = "Shrimp"
 menu[1] = "Gumbo"
 etc...
 cout << menu[0]
 etc...

However I am unable to store data in the array like that as well. How the heck am I supposed to store data in this array? As part of the assignment, the array must be a const static, it must be in private, and the displayMenu must also be private, called by a public function.

I hope what I'm getting at is clear, if not I'll try to provide more information.

EDIT: I can not edit anything from the instructors source file. The source file is already created, and he will be using his own (provided) to test both my class file and my header file. Thus it needs to be initiated outside of sourcefile.

Podo
  • 709
  • 9
  • 30
  • 2
    What didn't you understand from `static const`? You can't change constants. – πάντα ῥεῖ Mar 08 '16 at 18:36
  • I'm a bit confused I suppose. I need to have the array as a static const, as per the assignment, but I need to input values into the cells. How do I do that? – Podo Mar 08 '16 at 18:37
  • Why would someone edit out "Good morning SO" and "Thanks a heap, ~jef" from my post? – Podo Mar 08 '16 at 18:38
  • Do you really need that to be a `static` array? – πάντα ῥεῖ Mar 08 '16 at 18:38
  • 2
    Possible duplicate: [Initializing private static members](http://stackoverflow.com/questions/185844/initializing-private-static-members) – NathanOliver Mar 08 '16 at 18:38
  • "You are to create two const static, parallel arrays for the menu items, one for the item descriptions (string) and the second for the item costs (double), which, of course, will be class private data members." I do. – Podo Mar 08 '16 at 18:38
  • maybe you need a const pointer to non-const data? would that qualify as a const array? – johnbakers Mar 08 '16 at 18:38
  • 3
    @JeffreyDilley _"Why would someone edit out "Good morning SO" and "Thanks a heap, ~jef" from my post?"_ Because that's unnecessary noise, and it's not _morning_ here. – πάντα ῥεῖ Mar 08 '16 at 18:39
  • @ πάντα ῥεῖ , fair enough. @ NathanOliver, the difference is this is an array, and that post is not. – Podo Mar 08 '16 at 18:40
  • Possible duplicate: http://stackoverflow.com/questions/2117313/initializing-constant-static-array-in-header-file – AkiRoss Mar 08 '16 at 18:41
  • EDIT: I can not edit anything from the instructors source file. The source file is already created, and he will be using his own (provided) to test both my class file and my header file. Thus it needs to be initiated outside of sourcefile. Thanks for looking though! @AkiRoss – Podo Mar 08 '16 at 18:42
  • @JeffreyDilley ok, but the approach is the same: if you look at the answers, you see that it is the same problem. – AkiRoss Mar 09 '16 at 17:31

2 Answers2

2

You are falling into the same trap as many before you. Your array is const, so it must be initialized when declared. But you can not initialize static string arrays in the class body. What do you do? You initialize it outside!

like this:

in your .h file:

static const std::string menu[10];

in your .cpp file:

const std::string RestaurantCheck::menu[10] = {"Shrimp", "Calamari", "Listeria"};
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • @ SergeyA, forgive me, but what is the std:: used for? – Podo Mar 08 '16 at 18:46
  • 2
    @JeffreyDilley, since you should never have following line in your code: `using namespace std` (**NEVER**) - this allows compiler to understand I am talking about a string class from standard library. – SergeyA Mar 08 '16 at 18:47
  • I have used std in every single program I created. I never knew! You learn something new every day. – Podo Mar 08 '16 at 18:48
  • Could I define the array within a class function outside of my .h file? Say I prototype in my class, and I have restaurant.cpp. Can I build the class function in my cpp and initialize it there, or does it have to be entirely outside of the class? – Podo Mar 08 '16 at 18:49
  • @JeffreyDilley, you might read following question on SO: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice Yes, you can define an array within the class function if you want. – SergeyA Mar 08 '16 at 18:52
0

Is this what you are looking for?

You can initialize static non scalar members, but this must be done outside the class:

#include <string>

class RestaurantCheck{
    static const int MENU_LENGTH = 3;
    static const std::string menu[MENU_LENGTH];
};

const std::string RestaurantCheck::menu[RestaurantCheck::MENU_LENGTH] = {"Gumbo", "Shrimp", "Jar" };

note the "init line" must be present only in one file, best place is some .cpp file that is compiled to object. Here is what I mean:

restaurantcheck.h   - RestaurantCheck header
restaurantcheck.cpp - RestaurantCheck implementation (best place for "init line")
main.cpp            - program (where main() is located)
Nick
  • 9,962
  • 4
  • 42
  • 80