1

For context, I am brand new in C++, I program in Java. So I am very bad with C++ syntax and would require dumbed down answers.

I cannot seem to understand why I am getting this error. I checked multiple answers but they are all for primitive variables. I am using objects, which I am guessing is causing some error or I just am blind.

Here is my class, the focus is on 3 static variables under public

class SuperMarket
{
    private:
        int count;
        int totalService;
        int totalWait;
    public:
        static CustomerQ * regularLine; // this
        static CustomerQ * expressLine; // this
        static EventQ * eventQueue; // this
    // Constructors
        SuperMarket();
    // Destructor
        ~SuperMarket();
    // Accessors
        void start(int choice, string file);
        static void loop();
    // Mutators
};

I initialize the static members in my constructor

SuperMarket::SuperMarket() // Constructor
{
    count = 0;
    totalService = 0;
    totalWait = 0;
    regularLine = new CustomerQ(); *error*
    expressLine = new CustomerQ(); *error*
    SuperMarket::eventQueue = new EventQ(); *error*
}

As well as anywhere else I do SuperMarket::Object I get the error. I tried using both SuperMarket:: and without as you can see, but my error doesn't go away. Of course outside my SuperMarket class I do SuperMarket:: as well.

BadCoder
  • 141
  • 1
  • 16
  • Appears that `CustomerQ` and `EventQ` have not been declared. Also, since they are static to the class, it is very odd to set them in the instance's constructor. – Eljay Feb 28 '18 at 20:13
  • You need to get out of the habit of using `new` unless absolutely necessary (such as linked lists). This one major difference between Java and C++. BTW, C++ does not have garbage collection. – Thomas Matthews Feb 28 '18 at 20:29
  • Yes, for more context these are Queues, and PriorityQueues (secretly a sorted linked list) – BadCoder Feb 28 '18 at 20:30

2 Answers2

3

You only declared your static class members; you need to define them outside of the class the same way you would do with functions.

In your SuperMarket source file:

// You probably want to initialize outside of the constructor, because otherwise it would
// erase your queues whenever a new instance of SuperMarket is created.
CustomerQ* SuperMarket::regularLine = new CustomerQ();
CustomerQ* SuperMarket::expressLine = new CustomerQ();
EventQ* SuperMarket::eventQueue = new EventQ();

SuperMarket::SuperMarket() // Constructor
{
    // Probably don't initialize your statics in here
    // ...

However, in this case I don't think it makes sense for those members to be static. Static indicates that they should be shared across all instances of the containing class; does it really make sense for different SuperMarkets to all share the same lines of customers?

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Thank you so much it worked, I am using static because I plan on having other classes access the exact same queue. *edit* I will only have 1 SuperMarket as well – BadCoder Feb 28 '18 at 20:24
  • @TeenCoder I'd suggest following the singleton pattern here rather than some hybrid singleton that you have here. In other words make a single static Supermarket and make these static vars into members. – Sandy Chapman Feb 28 '18 at 20:41
  • @SandyChapman When I declared SuperMarket, I declared it as a static. Is that bad programming practice? – BadCoder Feb 28 '18 at 20:48
  • @TeenCoder I'd suggest doing some investigation around the singleton pattern. Some consider it an antipattern but it is still very common and when you in fact only need one instance it's an easy way to achieve it. More info https://stackoverflow.com/a/271104/1270148 – Sandy Chapman Feb 28 '18 at 20:51
2

In C++ (and in Java), keyword static preceding a member variable denotes a class member, i.e. a variable that exists once for the class, and not once for each instance of this class.

In C++ (in contrast to Java), you have to define a class member additionally outside the class definition, i.e.:

class SuperMarket {
        ...
        static CustomerQ * regularLine; // this
        ...
};

// .cpp-file:

CustomerQ * SuperMarket::regularLine = nullptr;

SuperMarket::SuperMarket() // Constructor
{
    count = 0;
    ...
}

Note, however, that assigning new objects to "class members" (i.e. static data members that exist only once in the program) within the constructor (i.e. with each object created) seldom makes sense.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58