0
class Scheduler:public Kolejka
{
private:
unsigned long real_time;
Scheduler(void) :real_time(0L){}
Scheduler(const Scheduler &);
Scheduler& operator=(const Scheduler&);
~Scheduler() {}

public:
std::deque <Kolejka*> kolejka;
//...                            rest of methods, not important here
static Scheduler& getInstance()
{
    unsigned long tmptime = 1;
    for (int i = 0; i < 10; i++)
    {
        Kolejka* tmp = new Kolejka(tmptime + 1);
        kolejka.push_back(tmp); //error C2228: left of '.push_back' must
                                //have class/struct/union
                    //IntelliSense: a nonstatic member reference must be relative 
                   //to a specific object
        tmptime++;          
        delete tmp;
    }
    static Scheduler instance;
    return instance;
}
};

The problem is written in code, i understand that i should do it the other way, but how? I'm asking for a little help :) I don't know how to get rid of this problem, i tried without pointers, but problem was the same.

edit: i solved it this way:

    void setscheduler()
    {
        unsigned long tmptime = 1;
        for (int i = 0; i < 10; i++)
        {
            Kolejka * tmp = new Kolejka(tmptime + 1);
            kolejka.push_back(tmp);
            tmptime++;
        }
    }
    static Scheduler& getInstance()
    {
        static Scheduler instance;
        return instance;
    }
    };

and i just make an empty deque, filling it a little bit later by

    Scheduler::getInstance().setscheduler();
Hrabia
  • 19
  • 7

1 Answers1

0

You can't access "non static" members of your class from static method. Static method does not know anything about "object" that you are thinking you are working with.

In order to implement it make a local variable of type Scheduler inside your getInstance() function and edit that variable. Then return it.

Also in singleton you have to return already existing instance if there was was created earlier and not create new one every time.

Hooch
  • 28,817
  • 29
  • 102
  • 161