how to initialize a vector of queues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • June Ye
    New Member
    • Feb 2011
    • 14

    how to initialize a vector of queues

    Hello,

    I have some difficulties in understanding initializing vector of queues. Here is my code.

    Code:
    #include <queue>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    
    class Employee {
          public:
                 Employee(){};
    };
    
    
    typedef queue<Employee*> EmployeeQueue;
    typedef vector<EmployeeQueue*> QueueArray;
    #define MAX 3
    #define Elem 2
    
    
    class CallCenter {
          public:
                 CallCenter(){
                    QueueArray eq1 = QueueArray(MAX);
    
                    for(int i = 0; i < MAX; ++i){
                            eq1[i] = new EmployeeQueue();
                            for(int j = 0; j < Elem; ++j) {
                                    Employee *e = new Employee();
                                    eq1[i]->push(e);
                                    cout << "push to queue " << i << endl;
                            }
                    } 
                    cout << "finish initialize" << endl;    
                 }
                 void GetEmployee() {
                    cout << "start to pop" << endl;
                    for(int i = 0; i < MAX; ++i){
                            while(!eq1[i]->empty()) {
                               cout << "pop queue i ";
                               eq1[i]->pop();
                            }
                    } 
                 }              
                 
          private:
                  QueueArray eq1;
    };
    
    
    int main() {
        CallCenter *cc = new CallCenter();
        cout << "call GetEmployee" << endl;
        cc->GetEmployee();
        system("pause");
        return 0;
    }
    The output is:


    push to queue 0
    push to queue 0
    push to queue 1
    push to queue 1
    push to queue 2
    push to queue 2
    finish initialize
    call GetEmployee
    start to pop


    Then it crashes when trying to access the eq1[i].
    Thank you very much if someone can point out the problem..

    June
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    I believe line 23 is where the problem lies: by calling the constructor directly, the Queue-array object that is created is temporary, and only exists for the duration of that statement.
    The correct way of creating the object would be like this:
    Code:
    QueueArray eq1(MAX);

    Comment

    • June Ye
      New Member
      • Feb 2011
      • 14

      #3
      Thanks... however it crashed at the same place with the new change.

      Comment

      • YarrOfDoom
        Recognized Expert Top Contributor
        • Aug 2007
        • 1243

        #4
        Oh wait, my bad. By prepending that statement with QueueArray you're redefining eq1 locally in the constructor. Meaning that within the constructor, every reference to eq1 goes to the version that is local to the function, rather than the class-wide version.

        Replacing line 46 with the one I gave earlier and removing line 23 should do the trick (hopefully I'm right this time).

        Comment

        • June Ye
          New Member
          • Feb 2011
          • 14

          #5
          Thanks for replying.
          It doesn't compile.

          Comment

          • YarrOfDoom
            Recognized Expert Top Contributor
            • Aug 2007
            • 1243

            #6
            Looks like I was wrong twice, apologies for that.

            You could have eq1 as a QueueArray-pointer and use the new operator on line 23 (don't forget to delete it afterwards in the destructor of your class).

            I'll try to get someone to explain whether there is a way to do it without pointers to both you and me.

            Comment

            • June Ye
              New Member
              • Feb 2011
              • 14

              #7
              Thanks for helping out.

              Comment

              Working...