2013-10-23 49 views
0

作爲家庭作業的一部分,我應該編寫一個模擬雜貨店環境中的隊列的程序。鏈接頁面上解釋了full assignmentC++隊列模擬

我只有一個隊列時正在運行程序,並且正在嘗試根據賦值描述對其進行修改以處理多個隊列。但是,編譯時出現一些錯誤。

我知道這個問題與排隊的客戶有關;我只是不確定如何修改程序,因此它適用於多個隊列。

任何援助非常感謝!

錯誤消息:

qsim.cpp: In function 'int main()': 
qsim.cpp:64: error: request for member 'empty' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]' 
qsim.cpp:66: error: request for member 'dequeue' in 'line', which is of non-class type 'Queue [(((long unsigned int)(((long int)queuecount) - 1)) + 1u)]' 

主程序
主程序包括一個稱爲Queue類。我知道這個代碼是正確的,因爲它可以在不同的測試程序中完美地工作。

#include <cstdlib> 
#include <iostream> 
#include <iomanip> 
#include "queue.h" 
using namespace std; 

int shortest_queue(Queue q[], int queuecount) 
{ 
    int shortest = 0; 
    for (int i = 1; i < queuecount; ++i) 
    { 
     if(q[i].size() < q[shortest].size()) 
      shortest = i; 
    } 
    return shortest; 
} 

int queue_total(Queue q[], int queuecount) 
{ 
    int custcount = 0; 
    for (int i = 0; i < queuecount; ++i) 
     custcount += q[i].size(); 
    return custcount; 
} 

int main() 
{ 
    int trans_time = 0; 
    int count = 0; 
    int entry_time; 
    int wait_sum = 0; 
    int wait_time = 0; 
    int seed; 
    int ARV_PROB; 
    int MAX_TRANS_TIME; 
    int DURATION; 
    int queuecount; 
    int shortline; 
    int temp; 

    cout << "Enter these parameters of the simulation:" << endl; 
    cout << " The number of queue/server pairs: "; 
    cin >> queuecount; 
    Queue line[queuecount]; 
    cout << " The probability that a customer arrives in one tick (%): "; 
    cin >> ARV_PROB; 
    cout << " The maximum duration of a transaction in ticks: "; 
    cin >> MAX_TRANS_TIME; 
    cout << " The duration of the simulation in ticks: "; 
    cin >> DURATION; 
    cout << "Enter a random number seed: "; 
    cin >> seed; 
    srand(seed); 

    for (int time = 0; time < DURATION; ++time) 
    { 
     if (rand() % 100 < ARV_PROB) 
     { 
      shortline = shortest_queue(line, queuecount); 
      line[shortline].enqueue(time); 
     } 
     if (trans_time == 0) 
     { 
      if (!line.empty()) 
      { 
       entry_time = line.dequeue(); 
       temp = (time - entry_time); 
       if(temp > wait_time) 
        wait_time = temp; 
       wait_sum += (time - entry_time); 
       ++count; 
       trans_time = (rand() % MAX_TRANS_TIME) + 1; 
      } 
     } 
     else 
     { 
      --trans_time; 
     } 
     cout << setw(4) << time << setw(4) << trans_time << " " << line << endl; 
    } 

    cout << count << " customers waited an average of "; 
    cout << wait_sum/count << " ticks." << endl; 
    cout << "The longest time a customer waited was " << wait_time << " ticks." << endl; 
    cout << queue_total(line, queuecount) << " customers remain in the lines." << endl; 

    return 0; 
} 
+0

大多數情況下,您可能試圖通過''''''''''''''''應該使用' - >'來訪問'empty' /'dequeue'成員。 –

回答

2
Queue line[queuecount]; 

if (!line.empty()) 

line不是Queue。它是一個數組Queues,所以你必須調用empty()你要檢查的特定數組元素。